减少gtest的输出,看起来类似于cxxtest的输出 [英] Reducing output of gtest, to look similar to output of cxxtest

查看:291
本文介绍了减少gtest的输出,看起来类似于cxxtest的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组使用gtest作为框架的单元测试。

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

生成并执行,输出如下:

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

当使用cxxunit(当测试通过,当失败时的详细错误)时,我想让输出类似于输出。

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.

如何操作?

如果我不清楚,我正在寻找代码和确切的方法如何做到这一点。

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

应该是:

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.

对于彩色输出,开发人员实现了 ColoredPrintf ()函数。

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

至于我面临的特定问题是缺乏文档:

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


  • 如何在程序开始时获取有关整个测试套件的信息? (我想在 OnTestProgramStart()方法)

  • 如何获取有关测试失败的信息? ( OnTestProgramEnd()?或其他方法?)

  • 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?)

推荐答案

链接到示例的此答案将包含一些我缺少的信息的更多信息。
这个答案帮助我获得了种子值。

This answer linked to the example will more information with some information I was missing. This answer helped me get the seed value.

标题如下:

#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

源文件:

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

这篇关于减少gtest的输出,看起来类似于cxxtest的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆