简体   繁体   English

在Visual Studio中查看Google测试结果

[英]Viewing Google Test results within Visual Studio

Is there a way to view Google Test results within Visual Studio? 有没有办法在Visual Studio中查看Google测试结果? If yes, how? 如果有,怎么样?
I'm using Google Test 1.5.0 and Visual Studio 2010 我正在使用Google Test 1.5.0和Visual Studio 2010

Until now I've been using Google Test from the command line. 到目前为止,我一直在从命令行使用Google Test。
I've seen such integrations on other IDEs (eclipse...) but not yet in VS 我已经在其他IDE(eclipse ......)上看到了这样的集成,但在VS中还没有

Look at GoogleTestAddin - I think it's what you want. 看看GoogleTestAddin - 我认为这就是你想要的。
Quoting from the CodePlex description: 引自CodePlex描述:

GoogleTestAddin is an Add-In for Visual Studio 2008 and 2010. GoogleTestAddin是Visual Studio 2008和2010的加载项。

It makes it easier to execute/debug googletest functions by selecting them. 通过选择它们,可以更轻松地执行/调试googletest函数。

You'll no longer have to set the command arguments of your test application to execute only specified functions or tests. 您将不再需要将测试应用程序的命令参数设置为仅执行指定的函数或测试。

The googletest output is redirected to the Visual Studio output window. googletest输出被重定向到Visual Studio输出窗口。 On failed tests you can easily jump to the code by doubleclick the error message. 在失败的测试中,您可以通过双击错误消息轻松跳转到代码。

There is a pretty simple way to use a parallel the googletest's output for your unit tests. 有一种非常简单的方法可以使用googletest的输出并行进行单元测试。

In few words you can create your own Printer class which outputs results directly to the VisualStudio's output window. 简而言之,您可以创建自己的Printer类,将结果直接输出到VisualStudio的输出窗口。 This way seems more flexible than others, because you can control both the result's content (format, details, etc.) and the destination. 这种方式似乎比其他方式更灵活,因为您可以控制结果的内容(格式,详细信息等)和目的地。 You can do it right in your main() function. 你可以在main()函数中正确执行。 You can use more than one Printer at once. 您可以一次使用多个打印机。 And you can jump to the code by doubleclick the error message on failed tests. 您可以通过在失败的测试上双击错误消息来跳转到代码。

These are steps to do it: 这些是执行此操作的步骤:

  1. Create a class derived from ::testing::EmptyTestEventListener class. 创建一个派生自::testing::EmptyTestEventListener类的类。
  2. Override necessary functions. 覆盖必要的功能。 Use OutputDebugString() function rather than printf() . 使用OutputDebugString()函数而不是printf()
  3. Before RUN_ALL_TESTS() call, create an instance of the class and link it to the gtest's listeners list. RUN_ALL_TESTS()调用之前,创建该类的实例并将其链接到gtest的侦听器列表。

Your Printer class may look like the following: 您的Printer类可能如下所示:

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

Linking the printer to the listeners list: 将打印机链接到侦听器列表:

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

The approach is described in the sample #9 from the Googletest samples . 该方法在Googletest样本样本#9中描述。

For Visual Studio 2012 there is also an extension that provides a Test Adapter for Google Test in Visual Studio (thus integrates with Visual Studios Test Explorer): Google Test Adapter 对于Visual Studio 2012,还有一个扩展,在Visual Studio中为Google Test提供测试适配器(因此与Visual Studios Test Explorer集成): Google Test Adapter

You can use a post-build event. 您可以使用后期构建事件。 Here is a guide: 这是一个指南:
http://leefw.wordpress.com/2010/11/17/google-test-gtest-setup-with-microsoft-visual-studio-2008-c/ http://leefw.wordpress.com/2010/11/17/google-test-gtest-setup-with-microsoft-visual-studio-2008-c/

You can also configure an "External Tool" in Visual Studio's Tools menu, and use it to run the target path of your project. 您还可以在Visual Studio的“工具”菜单中配置“外部工具”,并使用它来运行项目的目标路径。 (Hint: Create a toolbar menu item to make it easier to invoke) (提示:创建工具栏菜单项以使其更容易调用)

Use the feature-rich Google Test Adapter provided on GitHub and through the VS gallery (or via the Extensions menu of VS). 使用GitHub上提供的功能丰富的Google Test AdapterVS库 (或通过VS的Extensions菜单)。 It currently supports VS2013 and VS2015, VS2012 support is coming soon. 它目前支持VS2013和VS2015,VS2012支持即将推出。

Disclaimer: I am one of the authors of that extension. 免责声明:我是该扩展的作者之一。

使用适用于Visual Studio 2013的GoogleTest RunnerGoogle Test Adapter作者甚至推荐它作为更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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