Reputation: 133
Will the TearDown function be called when the ASSERT_EQ fails in google test? Or it just cancel that test case and move to the next one without TearDown? This is because in my TearDown function, I need to do something to properly shutdown the test function, so I'm afraid that this ASSERT will make my test not independent.
Upvotes: 1
Views: 1192
Reputation: 61347
It's not hard to satisfy yourself that TearDown
is always run, regardless
of whether an ASSERT_...
macro fails:
testcase.cpp
#include <gtest/gtest.h>
#include <iostream>
struct foo : ::testing::Test
{
void SetUp()
{
std::cout << ">>>" << __PRETTY_FUNCTION__ << " was run " << std::endl;
}
void TearDown()
{
std::cout << ">>>" << __PRETTY_FUNCTION__ << " was run " << std::endl;
}
};
TEST_F(foo,bar)
{
ASSERT_EQ(1,0);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Compile and link:
g++ -o testcase testcase.cpp -lgtest -pthread
Run:
$ ./testcase
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from foo
[ RUN ] foo.bar
>>>virtual void foo::SetUp() was run
testcase.cpp:19: Failure
Expected equality of these values:
1
0
>>>virtual void foo::TearDown() was run
[ FAILED ] foo.bar (0 ms)
[----------] 1 test from foo (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] foo.bar
1 FAILED TEST
Upvotes: 2