'Disable whole test case in gtest
How can one disable a complete test case in gtest? (all the tests in a test case, not just individual tests)
The formatting proposed in the gtest doc is to organise the tests in the following fashion:
class1test.cpp:
Test(Class1Test, TestA)
{
...
}
Test(Class1Test, TestB)
{
...
}
...
class2test.cpp:
Test(Class2Test, TestA)
{
...
}
Test(Class2Test, TestB)
{
...
}
....
class3test.cpp
and so on...
I know that adding the prefix DISABLED_
to a test will prevent it from running (ex: Test(Class1Test, DISABLED_TestB)
)
But what if I want to disable all the tests in the test case Class1Test?
This post GoogleTest: How to skip a test? suggest to use gtest filters, but this seems a complicated solution for what I want to do. It gtest filters are indeed the only solution, where should I write a filter that disables a test case?
Solution 1:[1]
Running tests with --gtest_filter=-Class1Test.*
should skip all tests in Class1Test
test case. This does not seem much complicated.
Solution 2:[2]
You can write this in the main function:
testing::GTEST_FLAG(filter) = "-Class1Test.*";
before
RUN_ALL_TESTS();
If you made your tests in Microsoft Visual Studio you can do this: Open properties of your TestProject and set the line
--gtest_filter=-Class1Test.*
in the Configuration Properties / Debugging / Command Arguments
You can comment your code using this
/* your tests */
or this
#ifdef _0
your tests
#endif
Solution 3:[3]
Gtest documentation answer this question exactly - see extract below (taken from: https://github.com/google/googletest/blob/master/googletest/docs/advanced.md)
Temporarily Disabling Tests
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).
If you need to disable all tests in a test suite, you can either add DISABLED_
to the front of the name of each test, or alternatively add it to the front of
the test suite name.
For example, the following tests won't be run by googletest, even though they will still be compiled:
// Tests that Foo does Abc.
TEST(FooTest, DISABLED_DoesAbc) { ... }
class DISABLED_BarTest : public testing::Test { ... };
// Tests that Bar does Xyz.
TEST_F(DISABLED_BarTest, DoesXyz) { ... }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ks1322 |
Solution 2 | |
Solution 3 | schlebe |