At the end of every "file based integration" test, I want to clear a temp folder of associated files.
If the test fails, I want to leave the files there, so I can review the unexpected output.
Is there a way in the Google Test TearDown to check if the test has failed?
If you find yourself writing two or more tests that operate on similar data, you can use a test fixture. This allows you to reuse the same configuration of objects for several different tests. To create a fixture: Derive a class from ::testing::Test .
The docs for Google Test 1.7 suggest: If you have a broken test that you cannot fix right away, you can add the DISABLED_ prefix to its name. This will exclude it from execution. This is better than commenting out the code or using #if 0 , as disabled tests are still compiled (and thus won't rot).
gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).
TEST_P() is useful when you want to write tests with a parameter. Instead of writing multiple tests with different values of the parameter, you can write one test using TEST_P() which uses GetParam() and can be instantiated using INSTANTIATE_TEST_SUITE_P() . Example test. Follow this answer to receive notifications.
Is there a way in the Google Test TearDown to check if the test has failed?
Yes, you can do that be querying ::testing::Test::HasFailure()
in
the test cases of your fixture and using the result to tally failures in a counter member of
the fixture that can be queried in its TearDown()
. An elementary example:
#include <gtest/gtest.h>
#include <iostream>
struct Fixture : public ::testing::Test {
virtual void SetUp() {
fails = 0;
}
virtual void TearDown() {
if (fails > 0) {
std::cerr << "Fixture::TearDown sees failures" << std::endl;
}
}
unsigned fails;
};
TEST_F(Fixture, foo) {
EXPECT_EQ(1,0);
fails += ::testing::Test::HasFailure();
}
TEST_F(Fixture, bar) {
EXPECT_EQ(1,1);
fails += ::testing::Test::HasFailure();
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And output:
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from Fixture
[ RUN ] Fixture.foo
/home/imk/dev/so/gtest/main.cpp:19: Failure
Expected: 1
To be equal to: 0
Fixture::TearDown sees failures
[ FAILED ] Fixture.foo (0 sec)
[ RUN ] Fixture.bar
[ OK ] Fixture.bar (0 sec)
[----------] 2 tests from Fixture (0.001 sec total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
[ FAILED ] 1 test, listed below:
[ FAILED ] Fixture.foo
1 FAILED TEST
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With