GoogleTest:如何跳过测试?

时间:2011-08-26 16:55:53

标签: googletest

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

8 个答案:

答案 0 :(得分:134)

Google Test 1.7的docs 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) { ... }

答案 1 :(得分:62)

根据文档,你也可以run a subset of tests

运行测试的子集

  

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

     

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

     

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

不是最漂亮的解决方案,但它确实有效。

答案 2 :(得分:16)

这里的表达式包含其名称中包含字符串foo1或foo2的测试,并排除其名称中包含字符串bar1或bar2的测试:

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

答案 3 :(得分:8)

我更喜欢在代码中执行此操作:

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

答案 4 :(得分:6)

您现在可以使用GTEST_SKIP()宏在运行时有条件地跳过测试。例如:

TEST(Foo, Bar)
{
    if (blah)
        GTEST_SKIP();

    ...
}

请注意,这非常recent feature,因此您可能需要更新GoogleTest库才能使用它。

答案 5 :(得分:5)

如果需要跳过多个测试

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

答案 6 :(得分: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_ *。如果失败,将断言其余的测试。防止大量冗余内容被转储到控制台。

答案 7 :(得分:3)

我对条件测试有同样的需求,我想出了一个很好的解决方法。我定义了一个像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();
相关问题