Search code examples
unit-testingbazel

bazel test lacking SEGFAULT information


We are currently migrating from a CMake-based build to bazel. For unit-testing, we are using our own implemented framework.

When dealing with a SEGFAULT, ctest gives the following output:

The following tests FAILED:
    19 - SomeTest (SEGFAULT)
Errors while running CTest

However, when executing the exact same test with the exact same build-options and sources, the bazel output looks like:

//services/SomeTest:test                                                FAILED in 0.2s
  /root/.cache/bazel/_bazel_root/b343aed36e4de4757a8e698762574e37/execroot/repo/bazel-out/k8-fastbuild/testlogs/SomeTest/test/test.log

The other output is just the regular printout from the test, nothing regarding the SEGFAULT. Same goes for the contents of SomeTest/test/test.log.

I tried the following options to bazel test: --test_output=all, --test_output=errors, --verbose_test_summary, and --verbose_failures.

What am I missing here?


Solution

  • The output you're seeing comes from CTest, not from your application under test. If you want to see helpful information like that you'll need some testing framework to provide it to you. Here's a comparison between a vanilla test and a Catch2 test.

    Setup

    test_vanilla.cc

    int main() { return 1 / 0; }
    

    test_catch2.cc

    #include <catch2/catch.hpp>
    
    TEST_CASE("Hello") { REQUIRE(1 / 0); }
    

    WORKSPACE

    load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
    
    http_archive(
        name = "catch2",
        sha256 = "3cdb4138a072e4c0290034fe22d9f0a80d3bcfb8d7a8a5c49ad75d3a5da24fae",
        strip_prefix = "Catch2-2.13.7",
        urls = ["https://github.com/catchorg/Catch2/archive/v2.13.7.tar.gz"],
    )
    

    BUILD

    cc_test(
        name = "test_vanilla",
        srcs = ["test_vanilla.cc"],
    )
    
    cc_test(
        name = "test_catch2",
        srcs = ["test_catch2.cc"],
        defines = ["CATCH_CONFIG_MAIN"],
        deps = ["@catch2"],
    )
    

    Testing

    No test framework

    Now let's run the tests.

    ❯ bazel test //:test_vanilla
    [...]
    //:test_vanilla        FAILED in 0.3s
    

    test.log

    exec ${PAGER:-/usr/bin/less} "$0" || exit 1
    Executing tests from //:test_vanilla
    -----------------------------------------------------------------------------
    

    You can see that the test failed, because it did not return 0 (as it failed by illegally dividing by zero. If you have systemd-coredump installed (and coredumps enabled), you can get some info with

    ❯ coredumpctl -1 debug
    [...]
    Core was generated by `/home/laurenz/.cache/bazel/_bazel_laurenz/be59967ad4f5a83f16e874b5d49a28d5/sand'.
    Program terminated with signal SIGFPE, Arithmetic exception.
    #0  0x0000561398132668 in main ()
    (gdb)
    

    Catch2

    If you have a test framework like CTest or Catch2, it will provide more infos so you don't even need to check the coredump yourself. The test log will provide the problematic file and line as well as the signal.

    ❯ bazel test //:test_catch2
    [...]
    //:test_catch2        FAILED in 0.2s
    

    test.log

    exec ${PAGER:-/usr/bin/less} "$0" || exit 1
    Executing tests from //:test_catch2
    -----------------------------------------------------------------------------
    
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    test_catch2 is a Catch v2.13.7 host application.
    Run with -? for options
    
    -------------------------------------------------------------------------------
    Hello
    -------------------------------------------------------------------------------
    test_catch2.cc:3
    ...............................................................................
    
    test_catch2.cc:3: FAILED:
    due to a fatal error condition:
      SIGFPE - Floating point error signal
    
    ===============================================================================
    test cases: 1 | 1 failed
    assertions: 1 | 1 failed