// 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) { ... }
如果您能够访问更新版本的 Google Test (当前版本是 v1.12.1) ,请在评论中查看 jslmsca 建议的 GTEST_SKIP()宏,在另一个答案中查看 Peter Bloomfield。来自 Advanced 中的样本:
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
}
./foo_test Has no flag, and thus runs all its tests.
./foo_test --gtest_filter=* Also runs everything, due to the single match-everything * value.
./foo_test --gtest_filter=FooTest.* Runs everything in test case FooTest.
./foo_test --gtest_filter=*Null*:*Constructor* Runs any test whose full name contains either "Null" or "Constructor".
./foo_test --gtest_filter=-*DeathTest.* Runs all non-death tests.
./foo_test --gtest_filter=FooTest.*-FooTest.Bar Runs everything in test case FooTest except FooTest.Bar.
// Run a specific test only
//testing::GTEST_FLAG(filter) = "MyLibrary.TestReading"; // I'm testing a new feature, run something quickly
// Exclude a specific test
testing::GTEST_FLAG(filter) = "-MyLibrary.TestWriting"; // The writing test is broken, so skip it
我对条件测试也有同样的需求,所以我想出了一个很好的解决办法。我定义了一个类似于 TEST _ F 宏的宏 TEST _ C,但是它有第三个参数,即布尔表达式,在开始测试之前,在 main.cpp 中计算运行时。不执行计算 false 的测试。宏观经济形势很糟糕,但看起来:
#pragma once
extern std::map<std::string, std::function<bool()> >* m_conditionalTests;
#define TEST_C(test_fixture, test_name, test_condition)\
class test_fixture##_##test_name##_ConditionClass\
{\
public:\
test_fixture##_##test_name##_ConditionClass()\
{\
std::string name = std::string(#test_fixture) + "." + std::string(#test_name);\
if (m_conditionalTests==NULL) {\
m_conditionalTests = new std::map<std::string, std::function<bool()> >();\
}\
m_conditionalTests->insert(std::make_pair(name, []()\
{\
DeviceInfo device = Connection::Instance()->GetDeviceInfo();\
return test_condition;\
}));\
}\
} test_fixture##_##test_name##_ConditionInstance;\
TEST_F(test_fixture, test_name)
另外,在 main.cpp 中,您需要这个循环来排除评估 false 的测试:
// identify tests that cannot run on this device
std::string excludeTests;
for (const auto& exclusion : *m_conditionalTests)
{
bool run = exclusion.second();
if (!run)
{
excludeTests += ":" + exclusion.first;
}
}
// add the exclusion list to gtest
std::string str = ::testing::GTEST_FLAG(filter);
::testing::GTEST_FLAG(filter) = str + ":-" + excludeTests;
// run all tests
int result = RUN_ALL_TESTS();