google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.8k stars 10.15k forks source link

gtest should support output stream redirect #1930

Open ftrofin opened 6 years ago

ftrofin commented 6 years ago

The default reporter (PrettyUnitTestResultPrinter) writes straight to stdout via printf calls. I would like to have an API so I can provide my own std::ostream, like boost Test library: https://www.boost.org/doc/libs/1_68_0/libs/test/doc/html/boost_test/test_output/logging_api/log_ct_output_stream_redirection.html

This is useful when you use gtest as part of a bigger framework and you need to redirect to output somewhere else (or convert it on the fly to a different encoding for example).

If for running the tests I can at least work around the problem by writing an event listener and writing into my own stream, for listing the tests, there is no workaround since ListTestsMatchingFilter is not accessible and the listeners are not invoked for the list tests action.

gennadiycivil commented 6 years ago

Thank you very much for this report. The best way to approach this would be to create a proper PR and submit it for consideration.

ftrofin commented 6 years ago

Before we even talk about a PR let's have a conversation about how this should be approached. There are many ways in which this can be done but all of them imply lots of changes to the existing code. Here are two that come immediately to mind.

  1. Replace all printf calls with std::cout << ... The advantage of doing this is that it's very easy to redirect cout (much easier than stdout).
  2. Pass in the output stream as a parameter to RunAllTests(). Redirect all output to it.

I'm guessing I'm not the first one thinking about this and there might be already significant effort invested in the design of this feature. Let's discuss and agree on the best approach before spending lots of time to modify the code.

gennadiycivil commented 5 years ago

I dont see anyone from the community chiming in here. At the same time this has not been a requirement internally.

gennadiycivil commented 5 years ago

Lack of participation signals lack of interest from the community. Closing

DannyVlasenko commented 5 years ago

Lack of participation signals lack of interest from the community. Closing

How many people from the community should vote for this issue to make you reopen it?

ftrofin commented 5 years ago

So which approach should we take for this? See the two options I layed out above (or it can be a new one...)

milladgit commented 4 years ago

This would be a great feature in the embedded world! In many cases, we need to redirect the textual output to a variable and then return that content back to our desktop system in the near future. I second this.

Any workaround at this point?

CraigHutchinson commented 2 years ago

Any progress on this? I am running on Android and don't see any output from my application so I currently expect this is my issue as from what I understand printf() goes to dev/nul

gaspardpetit commented 1 year ago

@CraigHutchinson for Android, I have this PR that's been waiting for almost a year: https://github.com/google/googletest/pull/3864

For the printf case, I attempted a macro solution in 2018 which was closed because macros are evil: https://github.com/google/googletest/pull/1201 and I tried again with a gtest_printf solution last year but again, no interest to merge since: https://github.com/google/googletest/pull/3860