GoogleTest: How to skip a 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).
Example from the above documentation:
// 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) { ... }
If you have access to more recent versions of Google Test (the current version is v1.12.1), check out the GTEST_SKIP()
macro suggested by jslmsca in the comments and Peter Bloomfield in another answer. From the sample in advanced.md:
TEST(SkipTest, DoesSkip) {
GTEST_SKIP() << "Skipping single test";
EXPECT_EQ(0, 1); // Won't fail; it won't be executed
}
class SkipFixture : public ::testing::Test {
protected:
void SetUp() override {
GTEST_SKIP() << "Skipping all tests for this fixture";
}
};
// Tests for SkipFixture won't be executed.
TEST_F(SkipFixture, SkipsOneTest) {
EXPECT_EQ(5, 7); // Won't fail
}
Disable whole test case in gtest
Running tests with --gtest_filter=-Class1Test.*
should skip all tests in Class1Test
test case. This does not seem much complicated.
How to Skip a Portion of the ”Code” in the source code while doing GoogleTest
Dont rely on the compilation flags, without affecting the production code use the GMOCK methods to get rid off the while (1) loop , the code can go like below:
TESTCODE:
class test_common : public ::testing::Test {
};
TEST_F(test_common, create_common) {
Common commonObj();
ON_CALL(mock_if, GetBool())
.WillByDefault(Return(true));
EXPECT_CALL(mock_if, GetBool())
.Times(AtLeast(1))
.WillOnce(Return(true))
.WillOnce(Return(false));
commonObj. ProcessData ();
}
ABSTRACT CODE:
class AbstractIf {
public:
AbstractIf (void) = default;
virtual ~AbstractIf (void) = default;
virtual bool GetBool() = 0;
};
MOCK CODE:
class MockIf : public AbstractIf {
public:
MOCK_METHOD0(GetBool,bool());
};
SOURCE CODE:
class Common: public Thread {
public:
friend class test_common;
Common {
}
~Common () {
}
virtual void ThreadMain();
protected:
virtual void ProcessData(void);
AbstractIf *prov_fl_if_;
};
void Common::ProcessData(void) {
while (prov_fl_if_->GetBool()) { }
}
By this way we can skip the part of the code we want without affecting the production code
Is there a way to stop GoogleTest after a specific test is run, whether it passes or fails?
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.
How to signal to gtest that a test wants to skip itself
I came up with a simple yet acceptable solution:
Simply print an additional skip line myself using a macro:
#define CHECK_FEATURE_OR_SKIP(FEATURE_NAME) \
do{\
if(!TypeParam::hasFeature(FEATURE_NAME)) {\
std::cout << "[ SKIPPED ] Feature " << #FEATURE_NAME << "not supported" << std::endl;\
return;\
}\
} while(0)
Then I can simply use this macro:
TYPED_TEST_P(TheTest, ATest){
CHECK_FEATURE_OR_SKIP(MyFeatureXY);
// ... real test code goes here
}
The result will look as follows:
[ RUN ] XYZ/TheTest/0.ATest
[ SKIPPED ] Feature MyFeatureXY not supported
[ OK ] XYZ/TheTest/0.ATest (0 ms)
The only small flaw is that there is still an OK
line, but at least it is apparent that the test case was skipped and also the missing feature is displayed neatly. Another flaw is that a GUI test runner will not display the skip that neatly, but I don't care about this as I only use command line tools to run the test cases.
Xml Generation from Google test does not show skipped test cases
It's probably a version issue. I'm gussing it was added here. The tag testsuite
has skipped
for me:
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="3" failures="0" disabled="0" errors="0" time="0" timestamp="2022-05-13T17:38:54.980" name="AllTests">
<testsuite name="MyTestSuite" tests="3" failures="0" disabled="0" skipped="1" errors="0" time="0" timestamp="2022-05-13T17:38:54.980">
<testcase name="Test1" status="run" result="skipped" time="0" timestamp="2022-05-13T17:38:54.980" classname="MyTestSuite">
<skipped message="tests/gtest_demo/fib_test.cc:10
"><![CDATA[tests/gtest_demo/fib_test.cc:10
]]></skipped>
</testcase>
<testcase name="Test2" status="run" result="completed" time="0" timestamp="2022-05-13T17:38:54.981" classname="MyTestSuite" />
<testcase name="Test3" status="run" result="completed" time="0" timestamp="2022-05-13T17:38:54.981" classname="MyTestSuite" />
</testsuite>
</testsuites>
This is the example that I ran: https://godbolt.org/z/9aW7GrYfj
Make sure you are running the latest release.
googletest SetUpTestSuite() does not run
Several things:
- The example is
SetUpTestSuite
, notSetUpTestCase
. SetUpTestSuite
should be a static member.field
should be a static member of the class if used inSetUpTestSuite
.SetUpTestSuite
runs once per test suite, not once per test case.- If you want something to run once per test case, use
SetUp
, which is a non-static member function. SetUp
can then manipulate non-static member variables.
See this example that shows the usage of both functions:
class Game_test : public testing::Test {
protected:
Game_test() = default;
virtual ~Game_test() = default;
public:
static void SetUpTestSuite() {
std::cout << "========Beginning of a test suit ========" << std::endl;
static_field = std::string("AAAA");
}
void SetUp() override {
std::cout << "========Beginning of a test ========" << std::endl;
object_field = std::string("AAAA");
}
static std::string static_field;
std::string object_field;
};
std::string Game_test::static_field;
TEST_F(Game_test, Test1) {
EXPECT_EQ(static_field, std::string("AAAA"));
EXPECT_EQ(object_field, std::string("AAAA"));
// We change object_field, SetUpTestSuite cannot reset it back to "AAAA" because
// it only runs once at the beginning of the test suite.
static_field = std::string("BBBB");
// Although we change object_field here,
// SetUp will reset it back to "AAAA" at the beginning of each test case.
object_field = std::string("BBBB");
}
TEST_F(Game_test, Test2) {
EXPECT_EQ(static_field, std::string("BBBB"));
EXPECT_EQ(object_field, std::string("AAAA"));
}
Live example: https://godbolt.org/z/e6Tz1xMr1
How to run specific test cases in GoogleTest
You could use advanced options to run Google tests.
To run only some unit tests you could use --gtest_filter=Test_Cases1*
command line option with value that accepts the *
and ?
wildcards for matching with multiple tests. I think it will solve your problem.
UPD:
Well, the question was how to run specific test cases. Integration of gtest with your GUI is another thing, which I can't really comment, because you didn't provide details of your approach. However I believe the following approach might be a good start:
- Get all testcases by running tests with
--gtest_list_tests
- Parse this data into your GUI
- Select test cases you want ro run
- Run test executable with option
--gtest_filter
Related Topics
Opencv How to Select a Region of Image Irregularly with Mouse Event? C/C++
Why Calling Main() Is Not Allowed in C++
How to Calculate the Angle from Rotation Matrix
Finding "Dead Code" in a Large C++ Legacy Application
Where Are the Man Pages for C++
General Use Cases for C++ Containers
Why Can't Visual Studio Find My Dll
Derived Class with Non-Virtual Destructor
Sizeof in C++ Showing String Size One Less
C++ Error, Undefined Reference Class
Throw New Std::Exception VS Throw Std::Exception
C++ Pure Virtual Function Have Body
Differencebetween Using a Struct with Two Fields and a Pair
C++ Warning: Address of Local Variable
Does "Undefined Behaviour" Extend to Compile-Time
Why Does This If Condition Fail for Comparison of Negative and Positive Integers