【问题标题】:Create tests at run-time (google test)在运行时创建测试(谷歌测试)
【发布时间】:2013-10-10 05:07:32
【问题描述】:

我有一个需要测试的解析器。这个解析器有很多测试输入文件。通过将 Parser 的输出与相应的预生成文件进行比较来测试 Parser 的预期行为。

目前我正在测试中处理 YAML 文件以获取输入文件、预期文件及其描述(如果失败,将打印此描述)。

解析器的一些参数也应该在相同的输入上进行测试。

所以,我需要在测试中形成如下代码:

TEST(General, GeneralTestCase)
{
   test_parameters = yaml_conf.get_parameters("General", "GeneralTestCase");
   g_parser.parse(test_parameters);

   ASSERT_TRUE(g_env.parsed_as_expected()) << g_env.get_description("General", "GeneralTestCase");
}

TEST(Special, SpecialTestCase1)
{
   test_parameters = yaml_conf.get_parameters("Special", "SpecialTestCase1");
   g_parser.parse(test_parameters);

   ASSERT_TRUE(g_env.parsed_as_expected()) << g_env.get_description("Special", "SpecialTestCase1");
}

TEST(Special, SpecialTestCase2)
{
   test_parameters = yaml_conf.get_parameters("Special", "SpecialTestCase2");
   g_parser.parse(test_parameters);

   ASSERT_TRUE(g_env.parsed_as_expected()) << g_env.get_description("Special", "SpecialTestCase2");
}

很容易看到代码的重复。所以我觉得有一种方法可以自动化这些测试。

提前致谢。

【问题讨论】:

    标签: c++ automation automated-tests googletest


    【解决方案1】:

    使用value-parameterized tests:

    typedef std::pair<std::string, std::string> TestParam;
    
    class ParserTest : public testing::TestWithParam<TestParam> {};
    
    TEST_P(ParserTest, ParsesAsExpected) {
       test_parameters = yaml_conf.get_parameters(GetParam().first,
                                                  GetParam().second);
       g_parser.parse(test_parameters);
       ASSERT_TRUE(g_env.parsed_as_expected());
    }
    
    INSTANTIATE_TEST_CASE_P(
        GeneralAndSpecial,
        ParserTest,
        testing::Values(
            TestParam("General", "GeneralTestCase")
            TestParam("Special", "SpecialTestCase1")
            TestParam("Special", "SpecialTestCase2")));
    

    您可以从磁盘读取测试用例列表并将其作为向量返回:

    std::vector<TestParam> ReadTestCasesFromDisk() { ... }
    
    INSTANTIATE_TEST_CASE_P(
        GeneralAndSpecial,  // Instantiation name can be chosen arbitrarily.
        ParserTest,
        testing::ValuesIn(ReadTestCasesFromDisk()));
    

    【讨论】:

    • 最好在这里指出一些附加信息。测试的注册需要在调用::testing::InitGoogleTest(); 之前进行。由于ReadTestCasesFromDisk() 可能需要访问一些由 cmd 参数传递的参数,因此对::testing::InitGoogleTest(); 的调用可能需要移到主函数中的稍后位置。
    【解决方案2】:

    我在 google 单元测试中添加了两个类 DynamicTestInfoScriptBasedTestInfo 以及 RegisterDynamicTest 函数。它至少需要 C++17(尚未分析向后移植到 C++11 或 C++14)——它允许动态创建谷歌单元测试/在运行时比当前的谷歌 API 稍微简单一些。

    例如用法可能是这样的:

    https://github.com/tapika/cppscriptcore/blob/f6823b76a3bbc0ed41b4f3cf05bc89fe32697277/SolutionProjectModel/cppexec/cppexec.cpp#L156

    但这需要修改谷歌测试,例如看这个文件:

    https://github.com/tapika/cppscriptcore/blob/f6823b76a3bbc0ed41b4f3cf05bc89fe32697277/SolutionProjectModel/logTesting/gtest/gtest.h#L819

    我将尝试将更改合并到官方谷歌测试存储库。

    我还更改了向用户应用程序报告测试的方式(使用&lt;loc&gt; 标签) 但这需要为 Visual Studio 修改过的 google 测试适配器,有关更多信息,请参阅以下 youtube 视频以了解更多关于工作原理的说明。

    https://www.youtube.com/watch?v=-miGEb7M3V8

    使用较新的 GTA,您可以在测试资源管理器中获取文件系统列表,例如:

    【讨论】:

    • 这正是我要找的。你有没有尝试过合并?
    • 由于不同的原因,我在谷歌测试适配器和谷歌测试中都拒绝了拉取请求。与此同时,我也在进行下一阶段的单元测试——即重新编码/回放和验证相同的单元测试。一些想法写在这里:github.com/csoltenborn/GoogleTestAdapter/issues/102不幸的是还没有开源代码,如果你真的想要,可以将它们作为演示测试项目进入github。
    猜你喜欢
    • 2011-02-10
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2012-03-30
    • 2010-11-14
    • 2011-06-14
    相关资源
    最近更新 更多