Cannot test output on stderr and stdout using googletest.

11,051 views
Skip to first unread message

Damian Giebas

unread,
Jun 13, 2016, 12:38:56 PM6/13/16
to Google C++ Testing Framework
Hello

I want test simple function like f() write below and test stderr, stdout and abort() is calling.

#include
<gtest/gtest.h>
#include <iostream>

void
f()
{
    std::cout<<"Some text" <<std::endl;
    std::cerr<<"Some error" <<std::endl;
    abort();
}

TEST(A, B)
{
    ::testing::internal::CaptureStdout();
    ASSERT_DEATH(f(), "");
    std::string capturedStdout = ::testing::internal::GetCapturedStdout().c_str();
    EXPECT_STREQ("Some text", capturedStdout.c_str());
}

int main(int argc, char *argv[])
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}


But test doesn't pass

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from A
[ RUN      ] A.B
src
/test_temp.cpp:16: Failure
Value of: capturedStdout.c_str()
 
Actual: ""
Expected: "Some text"
[  FAILED  ] A.B (27 ms)
[----------] 1 test from A (27 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (28 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] A.B

 
1 FAILED TEST

What is wrong in my code? I expect test fail. Stderr output is not equal "" but test does not report that. Also failur on test stdout output is unexpected.  Any help will be appreciated.

Best Regards.

Samuel Benzaquen

unread,
Jun 13, 2016, 12:41:54 PM6/13/16
to Damian Giebas, Google C++ Testing Framework
First of all, you should not use APIs marked as "internal".
They are not part of the public API, are not supported and are subject to change at any time.

As for your question, you should be using the second parameter to ASSERT_DEATH to verify the output of the process.
That second parameter is a regex that will be matched against the output.
_Sam

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Damian Giebas

unread,
Jun 24, 2016, 12:08:09 PM6/24/16
to Google C++ Testing Framework, damian...@globallogic.com
Hi.

Thanks for answer. I know about second parameter to ASSERT_DEATH. But look at this example. It should fail on ASSERT_DEATH not on EXPECT_STR. Why it happens?

http://stackoverflow.com/questions/3803465/how-to-capture-stdout-stderr-with-googletest - first solution from second post with buffer redirection is better?

In google test public api exist some function to test stdout and stderr?

Best regards.
_Sam

To unsubscribe from this group and stop receiving emails from it, send an email to googletestframework+unsub...@googlegroups.com.

Samuel Benzaquen

unread,
Jun 24, 2016, 12:22:22 PM6/24/16
to Damian Giebas, Google C++ Testing Framework
On Fri, Jun 24, 2016 at 8:25 AM, 'Damian Giebas' via Google C++ Testing Framework <googletes...@googlegroups.com> wrote:
Hi.

Thanks for answer. I know about second parameter to ASSERT_DEATH. But look at this example. It should fail on ASSERT_DEATH not on EXPECT_STR. Why it happens?

ASSERT_DEATH() will run the expression in a child process.
Capture*() captures the stdout/err of this process and it will not include anything from child processes.

On the other hand, ASSERT_DEATH will also capture the output of the child process and test it against the regex argument.
 
http://stackoverflow.com/questions/3803465/how-to-capture-stdout-stderr-with-googletest - first solution from second post with buffer redirection is better?

In google test public api exist some function to test stdout and stderr?

No, those APIs are internal for testing gTest itself.
They do so by overriding the stdout/stderr file descriptors and forwarding the data into a file, which means that they interfere with the actual output of the binary.
They could forward the output to the actual stdout/err, but that is more complex and there is no reason to do so on these internal APIs.

It is rather trivial to capture std::cout/std::cerr (not stdout/stderr) on your side, if you want.
 
_Sam


To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages