Cmake Build and test with Qt and google test

I’m trying to build a Qt C++ Application with automated tests using google tests. What is going wrong right now?

This is the CmakeList.txt in the /test directory.

set( RBUILDTEST ${RBUILD}Tests )

file( GLOB TEST_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp )

include( FetchContent )

FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)

set( gtest_force_shared_crt ON CACHE BOOL "" FORCE )
FetchContent_MakeAvailable( googletest )
add_executable( ${RBUILDTEST} ${TEST_FILES} )
target_link_libraries( ${RBUILDTEST} gtest_main )
target_link_libraries( ${RBUILDTEST} ${RBUILDLIB} )
target_include_directories( ${RBUILDTEST} PRIVATE ${CMAKE_SOURCE_DIR} )

include( GoogleTest )
gtest_discover_tests( ${RBUILDTEST} )

This is currently my error when running Cmake on windows 10 Cmake minimum version is 3.12. Ithink I’m currently using 3.19, maybe 3.22.

cmd.exe /C "cd . && F:\Qt\Tools\mingw810_64\bin\g++.exe -g  test/CMakeFiles/RailwayBuilderTests.dir/RailwayBuilderTests_autogen/mocs_compilation.cpp.obj test/CMakeFiles/RailwayBuilderTests.dir/allTests.cpp.obj test/CMakeFiles/RailwayBuilderTests.dir/elementTest.cpp.obj -o test\RailwayBuilderTests.exe -Wl,--out-implib,test\libRailwayBuilderTests.dll.a -Wl,--major-image-version,0,--minor-image-version,0  lib/libgtest_maind.a  librailbuild.a  lib/libgtestd.a  F:/Qt/6.1.2/mingw81_64/lib/libQt6Widgets.a  F:/Qt/6.1.2/mingw81_64/lib/libQt6Gui.a  F:/Qt/6.1.2/mingw81_64/lib/libQt6Core.a  -lmpr  -luserenv  -ld3d11  -ldxgi  -ldxguid  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cmd.exe /C "cd /D D:\Projects\build-RailwayBuilder-Desktop_Qt_6_1_2_MinGW_64_bit-Debug\test && F:\Qt\Tools\CMake_64\bin\cmake.exe -D TEST_TARGET=RailwayBuilderTests -D TEST_EXECUTABLE=D:/Projects/build-RailwayBuilder-Desktop_Qt_6_1_2_MinGW_64_bit-Debug/test/RailwayBuilderTests.exe -D TEST_EXECUTOR= -D TEST_WORKING_DIR=D:/Projects/build-RailwayBuilder-Desktop_Qt_6_1_2_MinGW_64_bit-Debug/test -D TEST_EXTRA_ARGS= -D TEST_PROPERTIES= -D TEST_PREFIX= -D TEST_SUFFIX= -D NO_PRETTY_TYPES=FALSE -D NO_PRETTY_VALUES=FALSE -D TEST_LIST=RailwayBuilderTests_TESTS -D CTEST_FILE=D:/Projects/build-RailwayBuilder-Desktop_Qt_6_1_2_MinGW_64_bit-Debug/test/RailwayBuilderTests[1]_tests.cmake -D TEST_DISCOVERY_TIMEOUT=5 -D TEST_XML_OUTPUT_DIR= -P F:/Qt/Tools/CMake_64/share/cmake-3.19/Modules/GoogleTestAddTests.cmake""
CMake Error at F:/Qt/Tools/CMake_64/share/cmake-3.19/Modules/GoogleTestAddTests.cmake:77 (message):
  Error running test executable.

    Path: 'D:/Projects/build-RailwayBuilder-Desktop_Qt_6_1_2_MinGW_64_bit-Debug/test/RailwayBuilderTests.exe'
    Result: Exit code 0xc0000135

    Output:

Call Stack (most recent call first):
  F:/Qt/Tools/CMake_64/share/cmake-3.19/Modules/GoogleTestAddTests.cmake:173 (gtest_discover_tests_impl)

If I had to guess, I’d say the Qt libraries were not found on the PATH when the test executable was run as a post-build step to query the list of tests it defines. You could try deferring the time gtest_discover_tests() performs that query to test time instead of doing it as a post-build rule. At test time, you would have to have your PATH already set up correctly for the tests to be able to run, so getting the list of tests from the executable at test time is likely to be more reliable. You can achieve that by setting the CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE variable to PRE_TEST before calling gtest_discover_tests(), or pass DISCOVERY_MODE PRE_TEST in the arguments to gtest_discover_tests().

1 Like

It actually works on linux but not on windows.

Yes, that’s the clue. On Linux, RPATH would be allowing the various shared libraries to be found, but on Windows you don’t have that. That’s why I mentioned PATH, which is what Windows uses to find the DLLs it needs.

1 Like