2011-08-26 170 views
71

使用Google Test 1.6(Windows 7,Visual Studio C++)。我怎样才能关闭给定的测试? (又如何防止测试运行)。除了评论整个测试之外,我还有什么可以做的吗?GoogleTest:如何跳过测试?

回答

98

docs对于谷歌测试1.7 suggest

“如果你有一个破碎的测试,你不能修复向右走,你可以在DISABLED_前缀添加到它的名字这将从执行排除它。”

例子:

// 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) { ... } 
+0

只是觉得太和过滤器 – User

+0

@Bill,在你发表你的评论之前,我发现它......(我也把它当作答案)。然后我删除了我的评论,认为它已经过时了...但是这是一些非常好的信息! +1 – Kiril

46

run a subset of tests可以根据文档:

运行测试

默认情况下的一个子集,一个谷歌测试程序运行的所有测试用户已定义。 有时,您只想运行一部分测试(例如,调试 或快速验证更改)。如果您将GTEST_FILTER 环境变量或--gtest_filter标志设置为过滤器字符串,则Google Test将仅运行全名(格式为 TestCaseName.TestName)与过滤器匹配的测试。

过滤器的格式是以':'分隔的通配符模式列表(称为正模式),可选地后跟' - '和另一个':' - 分隔的模式列表(称为负模式) 。 A 测试与过滤器匹配当且仅当它匹配任何正数 模式但不匹配任何负数模式。

模式可能包含'*'(匹配任何字符串)或'?' (匹配任何 单个字符)。为了方便,过滤器'* -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. 

不是最漂亮的解决方案,但它的工作原理。

5

如果需要一个以上的测试可以跳过

--gtest_filter=-TestName.*:TestName.*TestCase 
4

对于另一种方法,你可以用你的测试在功能和正常使用条件检查在运行时只,如果你想执行它们。

#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时才运行一些测试。

从技术上说,双栈堆栈的东西不应该是一个单元测试,因为它依赖于系统。但是我无法进行任何集成测试,除非我已经测试过它们的工作,这确保它不会在代码错误时报告故障。

至于它的测试,我有存根对象模拟系统的支持dualstack(或缺乏)通过构造假套接字。

唯一的缺点是测试输出和测试次数会改变,这可能会导致监视成功测试次数的问题。

您也可以使用ASSERT_ *而不是EQUAL_ *。如果失败,断言将会进行其余的测试。防止大量冗余的东西被转储到控制台。

12

下面是包括测试他们的名字在他们foo1或foo2的,并排除测试他们的名字在他们的弦BAR1或BAR2字符串表达式:

--gtest_filter=*foo1*:*foo2*-*bar1*:*bar2* 
4

我喜欢做它在代码:

// 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 *”来测试/排除一组功能。

+0

这是一个很好的解决方案。如果过滤器是空白的,我使用它来默认排除一些测试。它们可以通过'export GTEST_FILTER ='*''启用。 – Timmmm

+0

事实上,这是行不通的,因为默认是“'*'”而不是“”。相反,我只是使用另一个覆盖过滤器的环境变量。 – Timmmm

0

我对条件测试有相同的需求,我想出了一个很好的解决方法。我定义了一个类似于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中,你需要这个循环,排除虚假评估测试:

// 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();