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
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.
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:
::testing::EmptyTestEventListener
class. OutputDebugString()
function rather than printf()
. 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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With