15

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.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
Yuan
  • 2,690
  • 4
  • 26
  • 38

6 Answers6

9

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.

Elliot Chance
  • 5,526
  • 10
  • 49
  • 80
6

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.

Simon
  • 778
  • 6
  • 18
6

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.

rold2007
  • 1,297
  • 1
  • 12
  • 25
Jens Ehrlich
  • 873
  • 2
  • 11
  • 19
1

I wrote Google Test Pretty Printer, a test listener / pretty printer for Google Test, to provide cleaner and more attractive console output for Google Test programs. It includes a --failures-only option that should do what you want.

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
0

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.

dtasev
  • 540
  • 6
  • 12
-1

According to the documentation you can change output using Test Events. Look here (there is also an example): https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#extending-googletest-by-handling-test-events

rold2007
  • 1,297
  • 1
  • 12
  • 25
psur
  • 4,400
  • 26
  • 36