【问题标题】:Viewing Google Test results within Visual Studio在 Visual Studio 中查看 Google 测试结果
【发布时间】:2011-03-01 13:05:24
【问题描述】:

有没有办法在 Visual Studio 中查看 Google 测试结果?如果是,怎么做?
我正在使用 Google Test 1.5.0 和 Visual Studio 2010

到目前为止,我一直在命令行中使用 Google Test。
我已经在其他 IDE(eclipse...)上看到过这样的集成,但在 VS 中还没有看到

【问题讨论】:

    标签: visual-studio googletest


    【解决方案1】:

    看看GoogleTestAddin - 我想这就是你想要的。
    引用 CodePlex 描述:

    GoogleTestAddin 是 Visual Studio 2008 和 2010 的插件。

    通过选择它们可以更轻松地执行/调试 googletest 函数。

    您将不再需要将测试应用程序的命令参数设置为仅执行指定的函数或测试。

    googletest 输出被重定向到 Visual Studio 输出窗口。 如果测试失败,您可以通过双击错误消息轻松跳转到代码。

    【讨论】:

      【解决方案2】:

      有一种非常简单的方法可以将 googletest 的输出并行用于单元测试。

      简而言之,您可以创建自己的 Printer 类,将结果直接输出到 VisualStudio 的输出窗口。这种方式似乎比其他方式更灵活,因为您可以控制结果的内容(格式、细节等)和目的地。您可以在 main() 函数中正确执行此操作。您可以一次使用多台打印机。您可以通过双击失败测试的错误消息来跳转到代码。

      以下是执行此操作的步骤:

      1. 创建一个派生自::testing::EmptyTestEventListener的类 班级。
      2. 覆盖必要的函数。使用OutputDebugString() 函数而不是printf()
      3. 在调用RUN_ALL_TESTS() 之前,创建该类的一个实例并将其链接到gtest 的侦听器列表。

      您的打印机类可能如下所示:

      // Provides alternative output mode which produces minimal amount of
      // information about tests.
      class TersePrinter : public EmptyTestEventListener {
        void outDebugStringA (const char *format, ...)
        {
              va_list args;
              va_start( args, format );
              int len = _vscprintf( format, args ) + 1;
              char *str = new char[len * sizeof(char)];
              vsprintf(str, format, args );
              OutputDebugStringA(str);
              delete [] str;
        }
      
        // Called after all test activities have ended.
        virtual void OnTestProgramEnd(const UnitTest& unit_test) {
          outDebugStringA("TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
        }
      
        // Called before a test starts.
        virtual void OnTestStart(const TestInfo& test_info) {
          outDebugStringA(
                  "*** Test %s.%s starting.\n",
                  test_info.test_case_name(),
                  test_info.name());
        }
      
        // Called after a failed assertion or a SUCCEED() invocation.
        virtual void OnTestPartResult(const TestPartResult& test_part_result) {
          outDebugStringA(
                  "%s in %s:%d\n%s\n",
                  test_part_result.failed() ? "*** Failure" : "Success",
                  test_part_result.file_name(),
                  test_part_result.line_number(),
                  test_part_result.summary());
        }
      
        // Called after a test ends.
        virtual void OnTestEnd(const TestInfo& test_info) {
          outDebugStringA(
                  "*** Test %s.%s ending.\n",
                  test_info.test_case_name(),
                  test_info.name());
        }
      };  // class TersePrinter
      

      将打印机链接到侦听器列表:

      UnitTest& unit_test = *UnitTest::GetInstance();
      TestEventListeners& listeners = unit_test.listeners();
      listeners.Append(new TersePrinter);
      

      该方法在the Googletest samples 中的sample #9 中进行了描述。

      【讨论】:

        【解决方案3】:

        您可以使用构建后事件。这是一个指南:
        http://leefw.wordpress.com/2010/11/17/google-test-gtest-setup-with-microsoft-visual-studio-2008-c/

        您还可以在 Visual Studio 的工具菜单中配置“外部工具”,并使用它来运行项目的目标路径。 (提示:创建一个工具栏菜单项以使其更易于调用)

        【讨论】:

        • 关于构建后事件路径的说明:失败的测试将导致构建失败。当我将测试拉入持续集成服务器 (Jenkins) 时,这给我带来了问题,因为 Jenkins 会报告失败的构建而不是失败的测试。
        【解决方案4】:

        对于 Visual Studio 2012,还有一个扩展,它为 Visual Studio 中的 Google 测试提供了一个测试适配器(因此与 Visual Studios 测试资源管理器集成): Google Test Adapter

        【讨论】:

          【解决方案5】:

          使用 on GitHubthrough the VS gallery 提供的功能丰富的 Google 测试适配器(或通过 VS 的 Extensions 菜单)。目前支持VS2013和VS2015,即将支持VS2012。

          免责声明:我是该扩展程序的作者之一。

          【讨论】:

            【解决方案6】:

            在 Visual Studio 2013 中使用 GoogleTest Runner,甚至被 authorGoogle Test Adapter 推荐为更好的替代方案。

            【讨论】:

              猜你喜欢
              • 2014-07-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-09-15
              • 1970-01-01
              • 1970-01-01
              • 2021-05-31
              • 1970-01-01
              相关资源
              最近更新 更多