Question

Is there any way (aside from stdout redirection) to avoid my code's error and warning messages to be sent to stdout when using google tests?

I'd like to just get the tear-down and output from gtest instead of having my stdout log trashed with my program's manually generated warnings and exceptions that I need to test.

Was it helpful?

Solution

Assuming all your tests use fixtures, and all your output is <iostream>-based, you could do the following:

using namespace std;

class SomeTest : public testing::Test {
protected:
    virtual void setUp() {
        storedStreambuf_ = cout.rdbuf(); 
        cout.rdbuf(nullptr);
    }

    virtual void tearDown() {
        cout.rdbuf(storedStreambuf_);
    }
private:
    streambuf* storedStreambuf_; 
};

This will suppress all output via cout during your test's run, it can be done the same way for cerr and clog.

In order to keep this DRY, you could write a common base class inheriting from testing::Test, and make all your fixtures based on that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top