简体   繁体   中英

How to manage test output with google test in Eclipse CDT?

I am developing C++ code with Eclipse CDT. For unit testing I use google test which I integrated into Eclipse via the "C++ Unit Testing" plugin. A problem now occurs when tests generate output to stdout, eg, as in the example below.

Test.cpp:

#include <iostream>
#include <string>

#include "gtest/gtest.h"

TEST(TestSuite, TestCase) {
    std::string s = "str";
    std::cout << s;
    ASSERT_STREQ(s.c_str(), s.c_str());
}

AllTest.cpp

#include "gtest/gtest.h"

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

After building and running a suitable run configuration (for "C/C++ Unit" with "Google Tests Runner"), the "C/C++ Unit" tab displays an error message on the top: "Unkown error during parsing Google Test module output: Unexpected test module output."

When I run the test inside Eclipse as an application, it passes and the console shows the correct output. Tests also pass with the plugin as long as they do not generate any output (eg, change the string in Test.cpp to the empty string, std::string s = ""; )

Is there any workaround to allow the execution of tests that generate output via the plugin? I know that I could, eg, write the output to an ostream and and set that to be an ofstream during testing. However, I would prefer to test without any changes to the source code. Any ideas?

I am not so used to programming in C++, so thanks a lot for any advice.

PS: I am using Eclipse Oxygen (4.7.0).

Apparently it is not a problem if a test produces console output as long as the output string ends with a newline character. Eg,

std::cout << "someoutput\n";

works fine.

When having multiple outputs, it seems to suffice that the last output of any test case ends with the newline character:

std::cout << "someoutput1";
...
std::cout << "someoutput2\n";

Note that (at least as of now) this works even if the test fails in between the two outputs. This is because in the event of a test failure gtest produces some additional output and the extension apparently only requires to match parts of that. Of course, this might possibly stop working with any updates to either gtest or the Unit Testing extension.

Anyways, for my purposes and probably those of some others these restrictions are acceptable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM