GoogleTest:如何跳过测试?

Use*_*ser 103 googletest

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

Bil*_*ill 149

Google Test 1.7 的文档提示:

"如果你有一个无法立即解决的破坏测试,你可以在其名称中添加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) { ... }
Run Code Online (Sandbox Code Playgroud)


Kir*_*ril 63

根据文档,您还可以运行测试的子集:

运行测试的子集

默认情况下,Google Test程序会运行用户定义的所有测试.有时,您只想运行一部分测试(例如,用于调试或快速验证更改).如果将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. 
Run Code Online (Sandbox Code Playgroud)

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


ash*_*osh 17

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

--gtest_filter=*foo1*:*foo2*-*bar1*:*bar2*
Run Code Online (Sandbox Code Playgroud)


pil*_*kch 9

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

// 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
Run Code Online (Sandbox Code Playgroud)

我可以注释掉两行来运行所有测试,取消注释第一行以测试我正在调查/工作的单个功能,或者如果测试被破坏但是我想测试其他所有内容,则取消注释第二行.
您还可以使用通配符并编写列表"MyLibrary.TestNetwork*"或"-MyLibrary.TestFileSystem*"来测试/排除一组功能.


Pet*_*eld 9

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

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

    ...
}
Run Code Online (Sandbox Code Playgroud)

请注意,这是一项非常新的功能,因此您可能需要更新GoogleTest库才能使用它。

  • `GTEST_SKIP()` 从 1.10.0 开始可用。 (4认同)

Vij*_*y C 5

如果需要跳过多个测试

--gtest_filter=-TestName.*:TestName.*TestCase
Run Code Online (Sandbox Code Playgroud)


Dav*_*hop 5

对于另一种方法,您可以将测试包装在一个函数中,并在运行时使用正常的条件检查来仅在需要时执行它们。

#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);
   }
}
Run Code Online (Sandbox Code Playgroud)

这对我很有用,因为我仅在系统支持双栈 IPv6 时才尝试运行一些测试。

从技术上讲,双栈的东西不应该是真正的单元测试,因为它取决于系统。但是在我测试它们无论如何都可以工作之前,我无法真正进行任何集成测试,这确保它不会在不是代码错误时报告失败。

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

唯一的缺点是测试输出和测试数量会发生变化,这可能会导致监控成功测试数量的问题。

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


小智 5

我对条件测试有同样的需求,我想出了一个很好的解决方法。我定义了一个宏 TEST_C ,它的工作方式类似于 TEST_F 宏,但它有第三个参数,它是一个布尔表达式,在测试开始之前在 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)
Run Code Online (Sandbox Code Playgroud)

此外,在您的 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();
Run Code Online (Sandbox Code Playgroud)