首页 文章

GoogleTest:如何跳过测试?

提问于
浏览
87

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

8 回答

  • 1

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

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

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

  • 120

    如果需要跳过多个测试

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

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

    #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

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

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

    docs for Google Test 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) { ... }
    
  • 5

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

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

  • 15

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

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

    Running a Subset of the Tests

    默认情况下,Google Test程序会运行用户定义的所有测试 . 有时,您只想运行一部分测试(例如,用于调试或快速验证更改) . 如果将GTEST_FILTER环境变量或--gtest_filter标志设置为过滤器字符串,则Google Test将仅运行其全名(以TestCaseName.TestName形式)与过滤器匹配的测试 . 过滤器的格式是':' - 分隔的通配符模式列表(称为正模式),可选地后跟一个' - '和另一个':' - 分隔模式列表(称为负模式) . 当且仅当它与任何正模式匹配但与任何负模式不匹配时,测试才匹配过滤器 . 模式可能包含''(匹配任何字符串)或'?' (匹配任何单个字符) . 为方便起见,过滤器' -NegativePatterns'也可以写成'-NegativePatterns' . 例如:./ foo_test没有标志,因此运行所有测试 .
    ./foo_test --gtest_filter = 由于单个匹配 - 所有值,也运行所有内容 .
    ./foo_test --gtest_filter = FooTest . *运行测试用例FooTest中的所有内容 .
    ./foo_test --gtest_filter = * Null *:构造函数运行全名包含“Null”或“Constructor”的任何测试 .
    ./foo_test --gtest_filter = - * DeathTest . *运行所有非死亡测试 .
    ./foo_test --gtest_filter = FooTest . * - FooTest.Bar在测试用例FooTest中运行除FooTest.Bar之外的所有内容 .

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

相关问题