При использовании вспомогательных функций в gtest, которые используют макросы ASSERT_ или EXPECT_, эта вспомогательная функция должна быть аннулирована.Однако я хотел бы также проверить эти ошибки в вызывающем тест-коде.
Существует макрос ASSERT_NO_FATAL_FAILURE, который помогает остановить вызывающий код в случае срабатывания ASSERT_, но я бы хотел расширить его с помощью надлежащей обработки сбоев EXPECT_ (читай: NonFatalFailures).Вот что я получил до сих пор:
#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
//
Как вы можете видеть из выходных данных: Использование нового макроса CHECK_FOR_FAILURES улучшает тестовый вывод: он сообщает вам, какая строка вызвала сбой, и предотвращает выполнениеtest после подтверждения assert.
Однако использование HasNonfatalFailure () недостаточно, как вы можете видеть в выводе для i = 0.Причина в том, что уже была нефатальная ошибка, и для i = 0 не было НОВОЙ нефатальной ошибки, но HasNonfatalFailure () возвращает true из-за старой ..: - (
Любая идея, какЯ могу избавиться от неправильного вывода i = 0?