当gtest的ASSERT_宏出现故障时,是否调用TearDown函数?

When there is a failure in ASSERT_ macro of gtest, the TearDown function will be called or not?

ASSERT_EQ测试google失败时会调用TearDown函数吗?或者它只是取消那个测试用例并在没有 TearDown 的情况下移动到下一个?这是因为在我的 TearDown 函数中,我需要做一些事情来正确关闭测试函数,所以我担心这个 ASSERT 会使我的测试不独立。

不难让自己满意 TearDown 总是 运行,无论 ASSERT_... 宏是否失败:

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();
}

编译并link:

g++ -o testcase testcase.cpp -lgtest -pthread

运行:

$ ./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