// 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) { ... }
5
我更喜欢在代码中执行此操作:
// 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
#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();
8 回答
您现在可以使用
GTEST_SKIP()
宏在运行时有条件地跳过测试 . 例如:请注意,这非常recent feature因此您可能需要更新GoogleTest库才能使用它 .
如果需要跳过多个测试
对于另一种方法,您可以将测试包装在一个函数中,并在运行时使用正常的条件检查,以便只在您需要时执行它们 .
这对我很有用,因为我只是在系统支持双栈IPv6时尝试运行一些测试 .
从技术上讲,双栈的东西不应该是单元测试,因为它取决于系统 . 但是我无法进行任何集成测试,直到我测试它们仍然工作,这确保它不会报告故障,而不是代码故障 .
至于它的测试,我有通过构造假套接字来模拟系统对dualstack(或缺少)的支持的存根对象 .
唯一的缺点是测试输出和测试次数会发生变化,这可能会导致监控成功测试次数的问题 .
您也可以使用ASSERT_ *而不是EQUAL_ * . 如果失败,将断言其余的测试 . 防止大量冗余内容被转储到控制台 .
这里的表达式包括其名称中包含字符串foo1或foo2的测试,并排除其名称中包含字符串bar1或bar2的测试:
docs for Google Test 1.7 suggest:
“如果您有一个无法立即修复的测试,您可以在其名称中添加DISABLED_前缀 . 这将使其无法执行 . ”
例子:
我更喜欢在代码中执行此操作:
我可以注释掉两行来运行所有测试,取消注释第一行来测试我正在调查/工作的单个功能,或者如果测试被破坏但是我想测试其他所有内容,则取消注释第二行 .
您还可以使用通配符并编写列表"MyLibrary.TestNetwork*"或"-MyLibrary.TestFileSystem*"来测试/排除一组功能 .
我对条件测试有同样的需求,我想出了一个很好的解决方法 . 我定义了一个像TEST_F宏一样工作的宏TEST_C,但是它有一个第三个参数,它是一个布尔表达式,在测试开始之前在main.cpp中计算运行时 . 不执行评估false的测试 . 这个宏很难看,但它看起来像:
此外,在main.cpp中,您需要此循环来排除评估false的测试:
根据文档,你也可以run a subset of tests:
Running a Subset of the Tests
不是最漂亮的解决方案,但它确实有效 .