0
votes

I am trying to integrate Ctest and googletest in my project. I included googletest in my project as described in https://crascit.com/2015/07/25/cmake-gtest/.

I created a new test folder with a CoreTest.cpp and a CMakeLists.txt. The CMakeLists contains:

ADD_EXECUTABLE(CoreTest CoreTest.cpp)
TARGET_LINK_LIBRARIES(CoreTest corelib gtest gmock)
ADD_TEST(NAME CoreTest COMMAND CoreTest)

CoreTest.cpp contains:

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

This fails:

1>------ Rebuild All started: Project: RUN_TESTS, Configuration: Debug x64 ------
1>  Test project
1>      Start 1: CoreTest
1>  1/1 Test #1: CoreTest .....................***Failed    1.52 sec
1>  
1>  0% tests passed, 1 tests failed out of 1

However if I comment the InitGoogleTest line then the test succeeds.

Any idea on what could be the problem here ?

Many thanks !

Thanks axalis! I tried to run from the command line and realized that the googletest dlls were not in my path, which was the problem.

I added multiple tests but I can only see the ctest output in MSVC:

1>------ Rebuild All started: Project: RUN_TESTS, Configuration: Debug x64 ------
1>  Test project 
1>      Start 1: CoreTest
1>  1/1 Test #1: CoreTest .....................   Passed    0.03 sec
1>  
1>  100% tests passed, 0 tests failed out of 1

Is there a way to see the googletest output like what I get from the console ?:

>CoreTest.exe
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from MathTest
[ RUN      ] MathTest.TwoPlusTwoEqualsFour
[       OK ] MathTest.TwoPlusTwoEqualsFour (0 ms)
[ RUN      ] MathTest.TwoPlusFourEqualsSix
[       OK ] MathTest.TwoPlusFourEqualsSix (0 ms)
[----------] 2 tests from MathTest (1 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (4 ms total)
[  PASSED  ] 2 tests.
1
What is the output, if you run the CoreTest command directly from the command line? (there might be a segfault, or other clues why the test fails - CMake test run only shows the fail/success state but not the actual errors ... or you can normally find the reports in the "Testing" folder)axalis
In particular, I'm not exactly sure about GTest now, but for example Boost tests fail if there are no actual tests to be run - and before InitGoogleTest was added, the executable might have succeeded because without it the check of "no test to run" wasn't done. So, make sure that you have at least one test in the code to be run.axalis
@Guillaume first a comment: RUN_TESTS target is generated by CMake and don't know if there is a way to control the options used in executing ctest command through RUN_TESTS. When you do run ctest on the command line, you can add -V to get all the output. Now my question: I have the same original problem you have. How did you fix it? Did you pass the gtest DLL path to CTest, say through a CMake variable? The only way I could get RUN_TESTS to work is if I copied the gtest DLLs to the install directory where all my app's DLLs reside. Thanks for respondingNameRakes
I added the gtest dll paths to my PATH environment variable. Then I used a ADD_CUSTOM_COMMAND to execute the tests during the build. Hope this helps!Guillaume

1 Answers

3
votes

The GoogleTest module provides two functions which can be used to show the result of each individual test defined within your test executable. gtest_add_tests() is the traditional method which scans your source file(s) at configure time, while gtest_discover_tests() (available from CMake 3.10) delays working out the set of tests until CTest is run and is more robust (e.g. it has no problem with things like type- or value-parameterised tests).

If using CMake 3.8 or earlier, the gtest_add_tests() function is provided by FindGTest rather than GoogleTest. If you need compatibility with older CMake versions, use FindGTest because it pulls in the GoogleTest module anyway. If you can require CMake 3.9 or later, use GoogleTest directly because you don't actually want to try to find gtest since you are incorporating it directly into your build. With 3.10 or later, your test can be defined like this:

include(GoogleTest)
gtest_discover_tests(CoreTest)