Configure gtest to show failed test only in console
Asked Answered
A

6

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.

Awestricken answered 7/7, 2011 at 2:56 Comment(0)
I
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.

Ileum answered 2/1, 2014 at 5:5 Comment(0)
G
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.

Geddes answered 26/3, 2012 at 12:34 Comment(0)
H
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.

Hymnist answered 26/11, 2021 at 10:27 Comment(0)
M
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.

Medin answered 8/3, 2017 at 3:26 Comment(0)
G
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.

Guerrilla answered 8/10, 2019 at 9:9 Comment(0)
N
-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

No answered 15/3, 2012 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.