c++googletest

Check for errors in gtest using void functions


When using helper functions in gtest which use the ASSERT_ or EXPECT_ macros then that helper function has to be void. However, I'd like to check for those errors within the calling test-code as well.

There is the ASSERT_NO_FATAL_FAILURE macro which helps to stop the calling code in case an ASSERT_ was triggered, but I'd like to extend that with proper handling of EXPECT_ failures (read: NonFatalFailures) as well. Here's what I got so far:

#include <gtest/gtest.h>

// A void test-function using  ASSERT_ or EXPECT_ calls should be encapsulated by this macro.
// Example: CHECK_FOR_FAILURES(MyCheckForEquality(lhs, rhs))
#define CHECK_FOR_FAILURES(statement)                                                              \
  ASSERT_NO_FATAL_FAILURE((statement));                                                            \
  EXPECT_FALSE(HasNonfatalFailure())

void TestHelperFunction(bool givenAssert, int givenExpect)
{
    ASSERT_TRUE(givenAssert);  // note: this is line 11 in my code
    EXPECT_EQ(givenExpect, 0); // note: this is line 12 in my code
}

TEST(FailureInFunctionTestNoChecks, noChecks)
{
    // note: this is line 17 in my code
    TestHelperFunction(true, 0);
    TestHelperFunction(true, 1);
    for (int i = 0; i < 3; ++i)
    {
        TestHelperFunction(true, i);
    }
    TestHelperFunction(false, 1);
    TestHelperFunction(true, 2);
}

TEST(FailureInFunctionTestWithChecks, withChecks)
{
    // note: this is line 30 in my code
    CHECK_FOR_FAILURES(TestHelperFunction(true, 0)) << "\n All good - will NOT be seen! \n";
    CHECK_FOR_FAILURES(TestHelperFunction(true, 1)) << "\n optional msg: First Expect failed \n";
    for (int i = 0; i < 3; ++i)
    {
        CHECK_FOR_FAILURES(TestHelperFunction(true, i)) << "\n optional msg: Expect failed for i=" << i << "\n";
    }
    CHECK_FOR_FAILURES(TestHelperFunction(false, 1)) << "this message will NOT be seen due to the assert";
    CHECK_FOR_FAILURES(TestHelperFunction(true, 2)) << "\n will not be seen because assert stops the test \n";
}

// This test creates the following output:

// Note: Google Test filter = *FailureInFunctionTest*
// [==========] Running 2 tests from 2 test cases.
// [----------] Global test environment set-up.
// [----------] 1 test from FailureInFunctionTestNoChecks
// [ RUN      ] FailureInFunctionTestNoChecks.noChecks
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
//   givenExpect
//     Which is: 1
//   0
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
//   givenExpect
//     Which is: 1
//   0
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
//   givenExpect
//     Which is: 2
//   0
// ./checked_test_failure.cpp:11: Failure
// Value of: givenAssert
//   Actual: false
// Expected: true
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
//   givenExpect
//     Which is: 2
//   0
// [  FAILED  ] FailureInFunctionTestNoChecks.noChecks (0 ms)
// [----------] 1 test from FailureInFunctionTestNoChecks (0 ms total)
// 
// [----------] 1 test from FailureInFunctionTestWithChecks
// [ RUN      ] FailureInFunctionTestWithChecks.withChecks
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
//   givenExpect
//     Which is: 1
//   0
// ./checked_test_failure.cpp:32: Failure
// Value of: HasNonfatalFailure()
//   Actual: true
// Expected: false
// 
//  optional msg: First Expect failed 
// 
// ./checked_test_failure.cpp:35: Failure
// Value of: HasNonfatalFailure()
//   Actual: true
// Expected: false
// 
//  optional msg: Expect failed for i=0
// 
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
//   givenExpect
//     Which is: 1
//   0
// ./checked_test_failure.cpp:35: Failure
// Value of: HasNonfatalFailure()
//   Actual: true
// Expected: false
// 
//  optional msg: Expect failed for i=1
// 
// ./checked_test_failure.cpp:12: Failure
// Expected equality of these values:
//   givenExpect
//     Which is: 2
//   0
// ./checked_test_failure.cpp:35: Failure
// Value of: HasNonfatalFailure()
//   Actual: true
// Expected: false
// 
//  optional msg: Expect failed for i=2
// 
// ./checked_test_failure.cpp:11: Failure
// Value of: givenAssert
//   Actual: false
// Expected: true
// ./checked_test_failure.cpp:37: Failure
// Expected: (TestHelperFunction(false, 1)) doesn't generate new fatal failures in the current thread.
//   Actual: it does.
// [  FAILED  ] FailureInFunctionTestWithChecks.withChecks (1 ms)
// [----------] 1 test from FailureInFunctionTestWithChecks (1 ms total)
// 
// [----------] Global test environment tear-down
// [==========] 2 tests from 2 test cases ran. (1 ms total)
// [  PASSED  ] 0 tests.
// [  FAILED  ] 2 tests, listed below:
// [  FAILED  ] FailureInFunctionTestNoChecks.noChecks
// [  FAILED  ] FailureInFunctionTestWithChecks.withChecks
// 
//  2 FAILED TESTS
// 

As you can see from the output: Using the new 'CHECK_FOR_FAILURES' macro improves the test-output: It tells you which line caused a failure and it prevents executing a test after an assert was hit.

However, using 'HasNonfatalFailure()' is not good enough as you can see in the output for i=0. The reason is there was a non-fatal error already and for i=0 there was no NEW non-fatal error but the HasNonfatalFailure() returns true due to the old one.. :-(

Any idea how I can get rid of the wrong i=0 output?


Solution

  • An obvious way using the suggestion of gtest docs is the following, but I'd rather do it without custom messages or the SCOPED_TRACE() macro

    #include <gtest/gtest.h>
    
    // A void test-function using  ASSERT_ or EXPECT_ calls with a custom message should be encapsulated
    // by this macro. Example: CHECK_FOR_FAILURES_MSG(MyCheckForEquality(counter, 42), "for counter=42")
    #define CHECK_FOR_FAILURES_MSG(statement, message)                                                 \
    {                                                                                                  \
        SCOPED_TRACE(message);                                                                         \
        ASSERT_NO_FATAL_FAILURE((statement));                                                          \
    }
    
    // A void test-function using  ASSERT_ or EXPECT_ calls should be encapsulated by this macro.
    // Example: CHECK_FOR_FAILURES(MyCheckForEquality(lhs, rhs))
    #define CHECK_FOR_FAILURES(statement) CHECK_FOR_FAILURES_MSG(statement, " <--  line of failure\n")
    
    void TestHelperFunction(bool givenAssert, int givenExpect)
    {
        ASSERT_TRUE(givenAssert);  // note: this is line 17 in my code
        EXPECT_EQ(givenExpect, 0); // note: this is line 18 in my code
    }
    
    TEST(FailureInFunctionTestNoChecks, noChecks)
    {
        // note: this is line 23 in my code
        TestHelperFunction(true, 0);
        TestHelperFunction(true, 1);
        for (int i = 0; i < 3; ++i)
        {
            TestHelperFunction(true, i);
        }
        TestHelperFunction(false, 1);
        TestHelperFunction(true, 2);
    }
    
    TEST(FailureInFunctionTestWithChecks, withChecks)
    {
        // note: this is line 36 in my code
        CHECK_FOR_FAILURES(TestHelperFunction(true, 0));
        CHECK_FOR_FAILURES(TestHelperFunction(true, 1));
        for (int i = 0; i < 3; ++i)
        {
            CHECK_FOR_FAILURES_MSG(TestHelperFunction(true, i), "for i=" + std::to_string(i) + "\n");
        }
        CHECK_FOR_FAILURES(TestHelperFunction(false, 1));
        CHECK_FOR_FAILURES(TestHelperFunction(true, 2));
    }
    
    // Note: Google Test filter = *FailureInFunction*
    //[==========] Running 2 tests from 2 test cases.
    //[----------] Global test environment set-up.
    //[----------] 1 test from FailureInFunctionTestNoChecks
    //[ RUN      ] FailureInFunctionTestNoChecks.noChecks
    //./checked_test_failure.cpp:18: Failure
    //Expected equality of these values:
    //  givenExpect
    //    Which is: 1
    //  0
    //./checked_test_failure.cpp:18: Failure
    //Expected equality of these values:
    //  givenExpect
    //    Which is: 1
    //  0
    //./checked_test_failure.cpp:18: Failure
    //Expected equality of these values:
    //  givenExpect
    //    Which is: 2
    //  0
    //./checked_test_failure.cpp:17: Failure
    //Value of: givenAssert
    //  Actual: false
    //Expected: true
    //./checked_test_failure.cpp:18: Failure
    //Expected equality of these values:
    //  givenExpect
    //    Which is: 2
    //  0
    //[  FAILED  ] FailureInFunctionTestNoChecks.noChecks (0 ms)
    //[----------] 1 test from FailureInFunctionTestNoChecks (0 ms total)
    //
    //[----------] 1 test from FailureInFunctionTestWithChecks
    //[ RUN      ] FailureInFunctionTestWithChecks.withChecks
    //./checked_test_failure.cpp:18: Failure
    //Expected equality of these values:
    //  givenExpect
    //    Which is: 1
    //  0
    //Google Test trace:
    //./checked_test_failure.cpp:38:  <--  line of failure
    //
    //./checked_test_failure.cpp:18: Failure
    //Expected equality of these values:
    //  givenExpect
    //    Which is: 1
    //  0
    //Google Test trace:
    //./checked_test_failure.cpp:41: for i=1
    //
    //./checked_test_failure.cpp:18: Failure
    //Expected equality of these values:
    //  givenExpect
    //    Which is: 2
    //  0
    //Google Test trace:
    //./checked_test_failure.cpp:41: for i=2
    //
    //./checked_test_failure.cpp:17: Failure
    //Value of: givenAssert
    //  Actual: false
    //Expected: true
    //Google Test trace:
    //./checked_test_failure.cpp:43:  <--  line of failure
    //
    //./checked_test_failure.cpp:43: Failure
    //Expected: (TestHelperFunction(false, 1)) doesn't generate new fatal failures in the current thread.
    //  Actual: it does.
    //Google Test trace:
    //./checked_test_failure.cpp:43:  <--  line of failure
    //
    //[  FAILED  ] FailureInFunctionTestWithChecks.withChecks (0 ms)
    //[----------] 1 test from FailureInFunctionTestWithChecks (0 ms total)
    //
    //[----------] Global test environment tear-down
    //[==========] 2 tests from 2 test cases ran. (0 ms total)
    //[  PASSED  ] 0 tests.
    //[  FAILED  ] 2 tests, listed below:
    //[  FAILED  ] FailureInFunctionTestNoChecks.noChecks
    //[  FAILED  ] FailureInFunctionTestWithChecks.withChecks
    //
    // 2 FAILED TESTS
    //