Reducing output of gtest, to look similar to output of cxxtest

不羁岁月 提交于 2019-12-02 03:30:27

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.

BЈовић

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;
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!