Visual Studio 2019 Test Explorer did not find c++ google tests

5.1k views Asked by At

I want the c++ unit test written in google test displayed in the VS 2019 test explorer.

The tests are setup correctly and can be executed. The results are shown in the VS debug console/commandline-like window. No error messages besides the test dependent messages are shown. I want start the tests form the test explorer and want to create test play lists.

I installed the google test adapter provided by the VS Installer. I followed guidelines and the suggested troubleshooting at TestAdapterForGoogleTest.

Does another way exist to get the google test to be dispalyed in the test explorer? What are other known incompatibilities with google test and the VS test explorer?

3

There are 3 answers

0
Attis On BEST ANSWER

I had the same problem as you. I'm having a main CMakeLists.txt, and another two CMakeLists.txt files in the subdirectories: one for the static library I'm testing and one for the test project itself. For making sure the tests appear in the Test Explorer I had to move enable_testing() from the test subdirectory into the main CMakeLists.txt.

option(MY_PROJECT_TESTS "Build unit tests" ON)

if(MY_PROJECT_TESTS)
    enable_testing()
    add_subdirectory("test")
endif()

Then in the test subdirectory, I'm setting up the GoogleTest environment, and adding test the following way:

set(GTEST_DIR "googletest/googletest" CACHE PATH "gtest directory")
include(GoogleTest)
set(gtest_force_shared_crt OFF CACHE BOOL "" FORCE)
add_subdirectory("googletest")

project(My_project_test)

if (WIN32)
    add_library(qtpcre STATIC IMPORTED)
    set_target_properties(qtpcre PROPERTIES
        IMPORTED_LOCATION_DEBUG ${QT5_DIR}/lib/qtpcre2d.lib
        IMPORTED_LOCATION_RELEASE ${QT5_DIR}/lib/qtpcre2.lib
    )
endif()

set(CommonTestLib
    Qt5::Core
    My_project
    gtest_main
)

if (WIN32)
    list(APPEND CommonTestLib
        Ws2_32.lib
        version.lib
        Netapi32.lib
        Userenv.lib
        Crypt32.lib
        Winmm.lib
        qtpcre
    )
endif()

add_executable (My_project_test test_main.cpp test_cases.cpp)

target_precompile_headers(My_project_test REUSE_FROM My_project)
target_link_libraries(My_project_test ${CommonTestLib})
gtest_add_tests(TARGET My_project_test EXTRA_ARGS --arg1 "${CMAKE_CURRENT_SOURCE_DIR}/data")

The very last line is important. Instead of gtest_add_tests, you can also use add_test. It needs different parameters but that works too when your goal is showing test cases in VS2019's Test Explorer.

The reason the above solution helped:

When you add enable_testing() to your top-level CMakeLists.txt file, it will generate a top-level CTestTestfile.cmake file in your build directory. This is needed by the Test Explorer to roll up all the test cases generated during the build process. If you've got a certain CMake hierarchy within your code structure you should have a similar one for CTest.

My top-level CTestTestfile.cmake file content:

# CMake generated Testfile for 
# Source directory: C:/Projects/myproject
# Build directory: C:/Projects/myproject/out/build/x86-Debug
# 
# This file includes the relevant testing commands required for 
# testing this directory and lists subdirectories to be tested as well.
subdirs("test")

The lower level CTestTestfile.cmake file content:

# CMake generated Testfile for 
# Source directory: C:/Projects/MyProject/test
# Build directory: C:/Projects/MyProject/out/build/x86-Debug/test
# 
# This file includes the relevant testing commands required for 
# testing this directory and lists subdirectories to be tested as well.
add_test(Environment.TestCommandLineArgument "C:/Projects/MyProject/out/build/x86-Debug/test/MyProject_test.exe" "--gtest_filter=Environment.TestCommandLineArgument" "--arg1" "C:/Projects/MyProject/test/data/")
set_tests_properties(Environment.TestCommandLineArgument PROPERTIES  _BACKTRACE_TRIPLES "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.19/Modules/GoogleTest.cmake;380;add_test;C:/Projects/MyProject/test/CMakeLists.txt;38;gtest_add_tests;C:/Projects/MyProject/test/CMakeLists.txt;0;")
add_test(MyProjectExampleCreatorDevice.TestCreateExampleImage "C:/Projects/MyProject/out/build/x86-Debug/test/MyProject_test.exe" "--gtest_filter=MyProjectExampleCreatorDevice.TestCreateExampleImage" "--arg1" "C:/Projects/MyProject/test/data/")
set_tests_properties(MyProjectExampleCreatorDevice.TestCreateExampleImage PROPERTIES  _BACKTRACE_TRIPLES "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.19/Modules/GoogleTest.cmake;380;add_test;C:/Projects/MyProject/test/CMakeLists.txt;38;gtest_add_tests;C:/Projects/MyProject/test/CMakeLists.txt;0;")
...
0
Andrew Robinson On

There are multiple extensions that seem like they should be used. If you follow the advice of https://learn.microsoft.com/en-us/visualstudio/test/how-to-use-google-test-for-cpp?view=vs-2022 you use the TestAdapterForGoogleTest from microsoft ? () There is another from Google. (Google test adapter). I have a project that used old versions of gtest. I installed this extension and things stopped working. uninstall it and restart. rebuild and things seemed to work again.

0
Menace On

for me upgrading from 1.8.1.3 to 1.8.1.4 using nuget broke my project. It switched Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1.3 to Microsoft.googletest.v140.windesktop.msvcstl.dyn.rt-dyn.1.8.1.3\build\native\Microsoft.googletest.v140.windesktop.msvcstl.dyn.rt-dyn.targets"

revert back to the previous worked for me. (test explorer is now back) I think just making sure you use the static version as opposed to the dynamic should work as well.