12

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

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

6 Answers6

7

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.

Ovaron95
  • 94
  • 1
  • 1
7

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Rom098
  • 2,445
  • 4
  • 35
  • 52
5

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

NobodysNightmare
  • 2,992
  • 2
  • 22
  • 33
5

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)

Rami A.
  • 10,302
  • 4
  • 44
  • 87
  • A note on going the post-build event route: a failing test will cause the build to fail. This caused me problems when I pulled the tests into a continuous integration server (Jenkins) because Jenkins would report a failed build rather than a failed test. – Joe Schrag Jun 17 '14 at 16:26
1

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.

csoltenborn
  • 1,127
  • 1
  • 12
  • 22
0

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

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148