[英]GoogleTest: How to skip a test?
使用 Google Test 1.6(Windows 7、Visual Studio C++)。 如何关闭给定的测试? (也就是我怎样才能阻止测试运行)。 除了注释掉整个测试,我还能做些什么吗?
如果您有一个无法立即修复的损坏测试,您可以在其名称中添加
DISABLED_
前缀。 这会将其排除在执行之外。 这比注释掉代码或使用#if 0
更好,因为禁用的测试仍在编译(因此不会腐烂)。
上述文档中的示例:
// 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 测试程序会运行用户定义的所有测试。 有时,您只想运行测试的子集(例如,用于调试或快速验证更改)。 如果您将 GTEST_FILTER 环境变量或 --gtest_filter 标志设置为过滤器字符串,Google Test 将仅运行全名(以 TestCaseName.TestName 的形式)与过滤器匹配的测试。
过滤器的格式是一个“:”分隔的通配符模式列表(称为正模式),可选地后跟一个“-”和另一个“:”分隔的模式列表(称为负模式)。 当且仅当它匹配任何正模式但不匹配任何负模式时,测试才匹配过滤器。
模式可能包含“*”(匹配任何字符串)或“?” (匹配任何单个字符)。 为方便起见,过滤器“*-NegativePatterns”也可以写成“-NegativePatterns”。
例如:
./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.
不是最漂亮的解决方案,但它有效。
您现在可以使用GTEST_SKIP()
宏在运行时有条件地跳过测试。 例如:
TEST(Foo, Bar)
{
if (blah)
GTEST_SKIP();
...
}
请注意,这是一个非常新的功能,因此您可能需要更新您的 GoogleTest 库才能使用它。
下面的表达式包含名称中包含字符串 foo1 或 foo2 的测试,并排除名称中包含字符串 bar1 或 bar2 的测试:
--gtest_filter=*foo1*:*foo2*-*bar1*:*bar2*
我更喜欢用代码来做:
// 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
我可以注释掉这两行以运行所有测试,取消注释第一行以测试我正在调查/工作的单个功能,或者如果测试被破坏但我想测试其他所有内容,则取消注释第二行。
您还可以通过使用通配符和编写列表“MyLibrary.TestNetwork*”或“-MyLibrary.TestFileSystem*”来测试/排除一组功能。
如果需要超过一项测试,则跳过
--gtest_filter=-TestName.*:TestName.*TestCase
对于另一种方法,您可以将测试包装在一个函数中,并在运行时使用正常的条件检查来仅在需要时执行它们。
#include <gtest/gtest.h>
const bool skip_some_test = true;
bool some_test_was_run = false;
void someTest() {
EXPECT_TRUE(!skip_some_test);
some_test_was_run = true;
}
TEST(BasicTest, Sanity) {
EXPECT_EQ(1, 1);
if(!skip_some_test) {
someTest();
EXPECT_TRUE(some_test_was_run);
}
}
这对我很有用,因为我仅在系统支持双栈 IPv6 时才尝试运行一些测试。
从技术上讲,双栈的东西不应该是真正的单元测试,因为它取决于系统。 但是在我测试它们无论如何都可以工作之前,我不能真正进行任何集成测试,这样可以确保在不是代码错误时它不会报告失败。
至于对它的测试,我有通过构造假套接字来模拟系统对双栈(或不支持)的支持的存根对象。
唯一的缺点是测试输出和测试数量会发生变化,这可能会导致监控成功测试数量的东西出现问题。
您也可以使用 ASSERT_* 而不是 EQUAL_*。 如果测试失败,断言将处理其余的测试。 防止大量多余的东西被转储到控制台。
我对条件测试也有同样的需求,我想出了一个很好的解决方法。 我定义了一个像 TEST_F 宏一样工作的宏 TEST_C,但它有第三个参数,它是一个布尔表达式,在测试开始之前在 main.cpp 中评估运行时。 不执行评估为假的测试。 宏很丑,但它看起来像:
#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 中,您需要此循环来排除评估为假的测试:
// 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.