I have a set of unit tests using gtest as framework.
Build, and execute, and the output looks like this :
This answer linked to the example will more information with some information I was missing. This answer helped me get the seed value.
The header looks like :
#ifndef UNITTESTS_CXXLISTENER_HPP
#define UNITTESTS_CXXLISTENER_HPP
#include "gtest/gtest.h"
class CxxTestPrinter : public ::testing::EmptyTestEventListener
{
virtual void OnTestProgramStart( const ::testing::UnitTest& unit_test );
virtual void OnTestIterationStart( const ::testing::UnitTest& unit_test, int iteration );
virtual void OnTestPartResult( const ::testing::TestPartResult& test_part_result );
virtual void OnTestEnd( const ::testing::TestInfo& test_info );
virtual void OnTestIterationEnd( const ::testing::UnitTest& unit_test, int );
virtual void OnTestProgramEnd( const ::testing::UnitTest& unit_test );
};
#endif
And the source file :
#include "unittests_cxxlistener.hpp"
#include <iostream>
namespace testing {
namespace internal
{
enum GTestColor
{
COLOR_DEFAULT,
COLOR_RED,
COLOR_GREEN,
COLOR_YELLOW
};
void ColoredPrintf(GTestColor color, const char* fmt, ...);
}
}
using namespace testing::internal;
void CxxTestPrinter::OnTestProgramStart( const ::testing::UnitTest& unit_test )
{
std::cout << "Executing unit tests in " << unit_test.original_working_dir() << std::endl;
}
void CxxTestPrinter::OnTestIterationStart( const ::testing::UnitTest& , int iteration )
{
std:: cout << "Executing unit tests iteration " << iteration << " : ";
}
void CxxTestPrinter::OnTestPartResult( const ::testing::TestPartResult& test_part_result )
{
if ( test_part_result.failed() )
{
std::cout << "\n\nFailure in " << test_part_result.file_name() << " : line " << test_part_result.line_number() << std::endl
<< test_part_result.summary() << std::endl << std::endl;
}
}
void CxxTestPrinter::OnTestEnd( const ::testing::TestInfo& test_info )
{
if ( test_info.result()->Passed() )
{
ColoredPrintf( COLOR_GREEN, "." );
}
}
void CxxTestPrinter::OnTestIterationEnd(const ::testing::UnitTest& unit_test, int )
{
if ( unit_test.Passed() )
{
ColoredPrintf( COLOR_GREEN, " OK\n" );
}
else
{
ColoredPrintf( COLOR_RED, " FAILED\n" );
}
}
void CxxTestPrinter::OnTestProgramEnd( const ::testing::UnitTest& unit_test )
{
if ( unit_test.Failed() )
{
std::cout << "Some unit tests failed. test_random_seed = 0x" << std::hex << unit_test.random_seed() << std::endl;
}
}
The documentation page you mention has a link to the sample code which answers your questions. You can enumerate test cases and tests in them in OnTestProgramStart()
or later in this manner:
UnitTest& unit_test = *UnitTest::GetInstance();
for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
const TestCase& test_case = *unit_test.GetTestCase(i);
for (int j = 0; j < test_case.total_test_count(); ++j) {
const TestInfo& test_info = *test_case.GetTestInfo(j);
fprintf(stdout, "%s.%s\n", test_case.name().c_str(),
test_info.name().c_str());
}
}
The information about the test status can indeed be collected in OnTestProgramEnd()
:
virtual void OnTestProgramEnd(const UnitTest& unit_test) {
fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
fflush(stdout);
}
The ColoredPrintf()
is not available externally; you may just copy its code into your project.