c++cbashcmakegoogletest

How to print Google Test output to a text file?


I have gtest up and running using code as shown below. I would like to print the test output to a text file as opposed to displaying it in the console. Is there a way of doing this?

I run the tests using cmake from the console: cmake CMakeLists.txt && make && ./runTests .

#include "cw-test.c"
#include <stdio.h>
#include <gtest/gtest.h>

TEST(InputValidationTest, ValidateEntryLine)
{
    ...
}

...

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

Solution

  • You can redirect the output of your runTests command to a file:

    cmake CMakeLists.txt && make && ./runTests > test_output.txt
    

    Also, see this which explains why you do not need the & I had used in my comment. As Awaken's answer says, the & redirects both the stdout and stderr to the same file. But since googletest output always goes to stdout you may leave out the &.