Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure gtest to show failed test only in console

Is there an option to show only failed tests? I had to switch to use Guitar to achieve this, but I miss command line tool.

like image 730
Yuan Avatar asked Jul 07 '11 02:07

Yuan


People also ask

How do I disable test cases in Gtest?

If you need to disable all tests in a test suite, you can either add DISABLED_ to the front of the name of each test, or alternatively add it to the front of the test suite name.

Does Gtest run tests in parallel?

gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).

What is death test in Gtest?

Since these precondition checks cause the processes to die, we call such tests death tests. More generally, any test that checks that a program terminates in an expected fashion is also a death test.


3 Answers

I ran into the same issue - as I'm sure many other people have. So I created this:

https://gist.github.com/elliotchance/8215283

Should be pretty much paste and play.

like image 91
Elliot Chance Avatar answered Oct 10 '22 02:10

Elliot Chance


There are two ways to achieve this.

first one is to write your own event listener:

https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#defining-event-listeners

Another way is to filter the input the googletest event listener receives.

For this approache you remove the current event listener and exchange it with your own

testing::TestEventListeners& listeners = testing::UnitTest::GetInstance()->listeners();
testing::TestEventListener* listener = listeners.Release(listeners.default_result_printer());
listeners.Append(new FailurePrinter(listener));

where FailurePrinter is your own event listener class.

This class should look like this

class FailurePrinter : public ::testing::TestEventListener {

public:
FailurePrinter(TestEventListener* listener) : TestEventListener() {_listener = listener;}

virtual void OnTestProgramStart(const UnitTest& unit_test);
virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration);
virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test);
virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test);
virtual void OnTestCaseStart(const TestCase& test_case);
virtual void OnTestStart(const TestInfo& test_info);
virtual void OnTestPartResult(const TestPartResult& result);
virtual void OnTestEnd(const TestInfo& test_info);
virtual void OnTestCaseEnd(const TestCase& test_case);
virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test);
virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test);
virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);
virtual void OnTestProgramEnd(const UnitTest& unit_test);

protected:
testing::TestEventListener* _listener;
};

Now you have to implement all the methods.

If you like the way googles event listener prints something, just delegate the call to the _listener.

Or you can modify the result. For example:

void FailurePrinter::OnTestPartResult(const TestPartResult& test_part_result)
{
  if (test_part_result.failed())
  {
      _listener->OnTestPartResult(test_part_result);
      printf("\n");
  }
}

will only print Testfailures.

like image 30
Jens Ehrlich Avatar answered Oct 10 '22 02:10

Jens Ehrlich


There is a built-in solution for this now:

your_test_binary --gtest_brief=1

See also the documentation. There is also the GTEST_BRIEF environment variable to configure this.

like image 4
Simon Avatar answered Oct 10 '22 00:10

Simon