Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make google test return 0 even when tests fail?

Tags:

c++

googletest

I am calling a googletest in the post-build step of a C++ project in VS 2012.

Naturally, when any of the tests fail, the googletest command returns failure (-1), and the entire project is marked as a failure by Visual Studio.

I do not want that. I want the googletest to be run, and I want to see the results in the output, but I do not want to fail the project if not all tests pass.

Is there any flag that I can pass into googletest so that it always returns success (zero)?

like image 962
PortMan Avatar asked Oct 02 '15 21:10

PortMan


People also ask

How do I turn off test Google test?

The docs for Google Test 1.7 suggest: If you have a broken test that you cannot fix right away, you can add the DISABLED_ prefix to its name. This will exclude it from execution. This is better than commenting out the code or using #if 0 , as disabled tests are still compiled (and thus won't rot).

Does Google test run tests in parallel?

gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).

What is InitGoogleTest?

The ::testing::InitGoogleTest method does what the name suggests—it initializes the framework and must be called before RUN_ALL_TESTS . RUN_ALL_TESTS must be called only once in the code because multiple calls to it conflict with some of the advanced features of the framework and, therefore, are not supported.

What is a death test?

Death Tests are test assertions to put in unit test code, just like ASSERT_EQ , that check that the program exists or crashes in a certain manner given some inputs. So in the program code we still use assert() or other traditional methods to close/crash the application.


1 Answers

Yes, you can make the test return 0 if you write your own main function.

I imagine you're linking your test executable with the special gtest_main library which is a very basic helper to allow you to avoid having to write your own main function.

It's pretty much just doing:

int main(int argc, char **argv) {
  printf("Running main() from gtest_main.cc\n");
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

The RUN_ALL_TESTS macro is the culprit which is returning -1, so all you need to do is stop linking with gtest_main and write your own main more like:

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

For more info on this topic, see the GoogleTest docs.

like image 55
Fraser Avatar answered Sep 30 '22 02:09

Fraser