Configure gtest to show failed test only in console

Viewed 5480

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.

6 Answers

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.

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.

If you want a quick and dirty Python 2/3 solution for only failed tests, with no external dependencies: https://gist.github.com/DTasev/a894e4727eeaa94541d90ea1a3cc71a7. It will show failed test + its output. Instruction to use in docstring at the top of file

It requires gtest's default output, so if you've changed that it won't work.

Related