Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

c++ - Reducing output of gtest, to look similar to output of cxxtest

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?)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
", 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
", unit_test.Passed() ? "PASSED" : "FAILED");
  fflush(stdout);
}

The ColoredPrintf() is not available externally; you may just copy its code into your project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...