在 Visual Studio 中查看 Google 测试结果

发布于 2024-10-19 21:01:58 字数 188 浏览 1 评论 0 原文

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

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

Is there a way to view Google Test results within Visual Studio? If yes, how?
I'm using Google Test 1.5.0 and Visual Studio 2010

Until now I've been using Google Test from the command line.
I've seen such integrations on other IDEs (eclipse...) but not yet in VS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

木槿暧夏七纪年 2024-10-26 21:01:58

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

GoogleTestAddin 是 Visual Studio 2008 和 2010 的插件。

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

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

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

Look at GoogleTestAddin - I think it's what you want.
Quoting from the CodePlex description:

GoogleTestAddin is an Add-In for Visual Studio 2008 and 2010.

It makes it easier to execute/debug googletest functions by selecting them.

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.
On failed tests you can easily jump to the code by doubleclick the error message.

新一帅帅 2024-10-26 21:01:58

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

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

执行此操作的步骤如下:

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

您的 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

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

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

该方法在 示例 #9,来自 Google 测试示例

There is a pretty simple way to use a parallel the googletest's output for your unit tests.

In few words you can create your own Printer class which outputs results directly to the VisualStudio's output window. 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. 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.
  2. Override necessary functions. Use OutputDebugString()
    function rather than printf().
  3. Before RUN_ALL_TESTS() call, create an instance of the class and link it to the gtest's listeners list.

Your Printer class may look like the following:

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

妥活 2024-10-26 21:01:58

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

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

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/

You can also configure an "External Tool" in Visual Studio's Tools menu, and use it to run the target path of your project. (Hint: Create a toolbar menu item to make it easier to invoke)

攒眉千度 2024-10-26 21:01:58

对于 Visual Studio 2012,还有一个扩展为 Visual Studio 中的 Google Test 提供测试适配器(从而与 Visual Studios Test Explorer 集成):
Google 测试适配器

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

白首有我共你 2024-10-26 21:01:58

使用 GitHubGoogle 测试适配器 “https://visualstudiogallery.msdn.microsoft.com/94c02701-8043-4851-8458-34f137d10874”rel="nofollow">通过 VS 库(或通过扩展菜单VS)。目前支持VS2013和VS2015,VS2012支持即将推出。

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

Use the feature-rich Google Test Adapter provided on GitHub and through the VS gallery (or via the Extensions menu of VS). It currently supports VS2013 and VS2015, VS2012 support is coming soon.

Disclaimer: I am one of the authors of that extension.

宛菡 2024-10-26 21:01:58

使用适用于 Visual Studio 2013 的 GoogleTest Runner,甚至推荐使用它作者Google 测试适配器 的 为更好的选择。

Use GoogleTest Runner for Visual Studio 2013, it is even recommended by author of Google Test Adapter as better alternative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文