当 gtest 的宏出现故障ASSERT_,将调用或不调用 TearDown 函数



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

难满足自己,TearDown无论ASSERT_...宏是否失败:

测试用例.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();
}

编译和链接:

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

最新更新