Domanda

I have a set of unit tests using gtest as framework.

Build, and execute, and the output looks like this :

gtest output

I'd like to have the output similar to the output when using cxxunit (. when test pass, detail error when it fails). If possibly, with colors.

How to do this?


If I wasn't clear, I am looking for code and exact way how to do this.


The output should be something like :

Executing dummy_tests .....
../code/app/unit_tests/unittest_dummy.cpp:25: Failure
Value of: 2
Expected: 1
..
FAILED

I am trying to use this example and create custom listener. But the example doesn't have colorful output, and it prints lots of messages to the output.

As for the colorful output, developers implemented ColoredPrintf() function.

As for the particular problem I am facing is lack of documentation :

  • how to get information about whole tests suite at the program start? (I guess in the OnTestProgramStart() method)
  • how to get information about whether a test failed? (OnTestProgramEnd()? or other method?)
È stato utile?

Soluzione 2

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;
    }
}

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top