I have a large number of unit tests run through GoogleTest. Currently one of them (call it fooTest) is failing when the full test suite is run, but passing when run alone. So one (or more then one) of the tests that run before fooTest is doing something that causes it to fail, which of course is a big nono for testing and I obviously want to find the culprit.
This is happening with default run conditions, so the test order is always the same. The fooTest is about half way through this run order and there are enough tests that the run time of the second half of the tests is significant, especially if running things multiple times.
So I want to set googletest to always stop the test run after fooTest is run - whether it passes or fails. I know I can do --gtest_break_on_failure, but if I do a test run that causes fooTest to pass I still want to stop right there and not go through everything else. I could run with debug and add a breakpoint after the test, but that also slows things down a little and feels less then ideal.
It seems like this could be an simple setting, but I've not been able to find anything yet. I'm hoping there is either a parameter on run I can use, or a trick to make this happen.
CodePudding user response:
You have several options:
Simply call exit function at the end of
fooTest
.Create a test fixture. In
SetUp
check for a flag that is always false, but it sets to true iffooTest
is executed. Something like this:
bool skip_testing = false;
// Class for test fixture
class MyTestFixture : public ::testing::Test {
protected:
void SetUp() override {
if (skip_testing) {
GTEST_SKIP();
}
}
};
TEST_F(MyTestFixture, test1) {
//
EXPECT_EQ(1, 1);
}
TEST_F(MyTestFixture, footest) {
EXPECT_EQ(1, 1);
skip_testing = true;
}
TEST_F(MyTestFixture, test2) {
//
EXPECT_EQ(1, 1);
}
TEST_F(MyTestFixture, test3) {
//
EXPECT_EQ(1, 1);
}
See a working a working example here: https://godbolt.org/z/8dzKGE6Eh
- Similar to Option 2, but use explicit success and failure in
SetUp
instead ofGTEST_SKIP
. However usingGTEST_SKIP
is preferred, cause your output will show that the tests were skipped.