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

前端 未结 2 1069
青春惊慌失措
青春惊慌失措 2020-12-22 04:02

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

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

2条回答
  •  悲哀的现实
    2020-12-22 04:14

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

提交回复
热议问题