简体   繁体   English

GoogleTest:如何跳过测试?

[英]GoogleTest: How to skip a test?

Using Google Test 1.6 (Windows 7, Visual Studio C++).使用 Google Test 1.6(Windows 7、Visual Studio C++)。 How can I turn off a given test?如何关闭给定的测试? (aka how can I prevent a test from running). (也就是我怎样才能阻止测试运行)。 Is there anything I can do besides commenting out the whole test?除了注释掉整个测试,我还能做些什么吗?

The docs for Google Test 1.7 suggest : Google Test 1.7 的文档建议

If you have a broken test that you cannot fix right away, you can add the DISABLED_ prefix to its name.如果您有一个无法立即修复的损坏测试,您可以在其名称中添加DISABLED_前缀。 This will exclude it from execution.这会将其排除在执行之外。 This is better than commenting out the code or using #if 0 , as disabled tests are still compiled (and thus won't rot).这比注释掉代码或使用#if 0更好,因为禁用的测试仍在编译(因此不会腐烂)。

Example from the above documentation:上述文档中的示例:

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

You can also run a subset of tests , according to the documentation:根据文档,您还可以运行测试的子集

Running a Subset of the Tests运行测试的子集

By default, a Google Test program runs all tests the user has defined.默认情况下,Google 测试程序会运行用户定义的所有测试。 Sometimes, you want to run only a subset of the tests (eg for debugging or quickly verifying a change).有时,您只想运行测试的子集(例如,用于调试或快速验证更改)。 If you set the GTEST_FILTER environment variable or the --gtest_filter flag to a filter string, Google Test will only run the tests whose full names (in the form of TestCaseName.TestName) match the filter.如果您将 GTEST_FILTER 环境变量或 --gtest_filter 标志设置为过滤器字符串,Google Test 将仅运行全名(以 TestCaseName.TestName 的形式)与过滤器匹配的测试。

The format of a filter is a ':'-separated list of wildcard patterns (called the positive patterns) optionally followed by a '-' and another ':'-separated pattern list (called the negative patterns).过滤器的格式是一个“:”分隔的通配符模式列表(称为正模式),可选地后跟一个“-”和另一个“:”分隔的模式列表(称为负模式)。 A test matches the filter if and only if it matches any of the positive patterns but does not match any of the negative patterns.当且仅当它匹配任何正模式但不匹配任何负模式时,测试才匹配过滤器。

A pattern may contain '*' (matches any string) or '?'模式可能包含“*”(匹配任何字符串)或“?” (matches any single character). (匹配任何单个字符)。 For convenience, the filter '*-NegativePatterns' can be also written as '-NegativePatterns'.为方便起见,过滤器“*-NegativePatterns”也可以写成“-NegativePatterns”。

For example:例如:

 ./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.

Not the prettiest solution, but it works.不是最漂亮的解决方案,但它有效。

You can now use the GTEST_SKIP() macro to conditionally skip a test at runtime.您现在可以使用GTEST_SKIP()宏在运行时有条件地跳过测试。 For example:例如:

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

    ...
}

Note that this is a very recent feature so you may need to update your GoogleTest library to use it.请注意,这是一个非常新的功能,因此您可能需要更新您的 GoogleTest 库才能使用它。

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

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

I prefer to do it in code:我更喜欢用代码来做:

// 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

I can either comment out both lines to run all tests, uncomment out the first line to test a single feature that I'm investigating/working on, or uncomment the second line if a test is broken but I want to test everything else.我可以注释掉这两行以运行所有测试,取消注释第一行以测试我正在调查/工作的单个功能,或者如果测试被破坏但我想测试其他所有内容,则取消注释第二行。
You can also test/exclude a suite of features by using wildcards and writing a list, "MyLibrary.TestNetwork*" or "-MyLibrary.TestFileSystem*".您还可以通过使用通配符和编写列表“MyLibrary.TestNetwork*”或“-MyLibrary.TestFileSystem*”来测试/排除一组功能。

如果需要超过一项测试,则跳过

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

For another approach, you can wrap your tests in a function and use normal conditional checks at runtime to only execute them if you want.对于另一种方法,您可以将测试包装在一个函数中,并在运行时使用正常的条件检查来仅在需要时执行它们。

#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);
   }
}

This is useful for me as I'm trying to run some tests only when a system supports dual stack IPv6.这对我很有用,因为我仅在系统支持双栈 IPv6 时才尝试运行一些测试。

Technically that dualstack stuff shouldn't really be a unit test as it depends on the system.从技术上讲,双栈的东西不应该是真正的单元测试,因为它取决于系统。 But I can't really make any integration tests until I have tested they work anyway and this ensures that it won't report failures when it's not the codes fault.但是在我测试它们无论如何都可以工作之前,我不能真正进行任何集成测试,这样可以确保在不是代码错误时它不会报告失败。

As for the test of it I have stub objects that simulate a system's support for dualstack (or lack of) by constructing fake sockets.至于对它的测试,我有通过构造假套接字来模拟系统对双栈(或不支持)的支持的存根对象。

The only downside is that the test output and the number of tests will change which could cause issues with something that monitors the number of successful tests.唯一的缺点是测试输出和测试数量会发生变化,这可能会导致监控成功测试数量的东西出现问题。

You can also use ASSERT_* rather than EQUAL_*.您也可以使用 ASSERT_* 而不是 EQUAL_*。 Assert will about the rest of the test if it fails.如果测试失败,断言将处理其余的测试。 Prevents a lot of redundant stuff being dumped to the console.防止大量多余的东西被转储到控制台。

I had the same need for conditional tests, and I figured out a good workaround.我对条件测试也有同样的需求,我想出了一个很好的解决方法。 I defined a macro TEST_C that works like a TEST_F macro, but it has a third parameter, which is a boolean expression, evaluated runtime in main.cpp BEFORE the tests are started.我定义了一个像 TEST_F 宏一样工作的宏 TEST_C,但它有第三个参数,它是一个布尔表达式,在测试开始之前在 main.cpp 中评估运行时。 Tests that evaluate false are not executed.不执行评估为假的测试。 The macro is ugly, but it look like:宏很丑,但它看起来像:

#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)

Additionally, in your main.cpp, you need this loop to exclude the tests that evaluate false:此外,在您的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM