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:
- Create a class derived from
::testing::EmptyTestEventListener
class.
- Override necessary functions. Use
OutputDebugString()
function rather than printf()
.
- 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.