microsoft / vcpkg

C++ Library Manager for Windows, Linux, and MacOS
MIT License
22.94k stars 6.34k forks source link

[GTest] - Not listing tests in VS2022 and CLI (windows) #26109

Closed Baklap4 closed 2 years ago

Baklap4 commented 2 years ago

Hello

We're trying to implement some unittesting with GTest. It works on linux, but then we wanted to try it out on Windows (through vcpkg) stuff didn't seem to work: The tests we wrote didn't end up listed and we got the error:

Test project C:/Users/Username/source/repos/performous/build/x64-debug
No tests were found!!!

Our project structure is:

|-- CMakeLists.txt
|-- Game
|------- CMakeLists.txt
|-- Testing
|------- CMakeLists.txt

So within the main CMakeLists.txt (in the root of the project) we defined the following:

enable_testing()
find_package(GTest)
if(GTest_FOUND)
    message(STATUS "Testing enabled: Building test host")
    add_subdirectory(testing)
else()
    message(STATUS "Testing disabled: Package gtest missing")
endif()

This allows us to find GTest (within vcpkg) and enable Testing within cmake.

So then for the Testing/CmakLists.txt we have defined the following:

cmake_minimum_required(VERSION 3.15)
include(GoogleTest)
set(SOURCE_FILES 
    "sometest.cc"
)
set(GAME_SOURCES 
    "../game/someclass.cc"
)

set(SOURCES ${SOURCE_FILES} ${HEADER_FILES} ${GAME_SOURCES})

add_executable(performous_test ${SOURCES})

if(WIN32)
    target_link_libraries(performous_test PRIVATE GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main)
else()
    target_link_libraries(performous_test PRIVATE GTest::GTest GTest::Main)
endif()

target_include_directories(performous_test PRIVATE "..")
set_target_properties(performous_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) 
gtest_discover_tests(performous_test)

We copied over the target_link_libraries command from the usage in the port description. Within our sometest.cc we depend on some functionality from gmock:

EXPECT_THAT(ConfigItem(std::vector<std::string>{}).ol(), IsEmpty());

the EXPECT_THAT and the IsEmpty() macro comes from the gmock-matchers.h header.

the full unittest in question:

TEST(UnitTest_ConfigItem, ol) {
        EXPECT_THAT(ConfigItem(std::vector<std::string>{}).ol(), IsEmpty());
        EXPECT_THAT(ConfigItem(std::vector<std::string>{"A"}).ol(), ElementsAre("A"));
        EXPECT_THAT(ConfigItem(std::vector<std::string>{"A", "B", "C"}).ol(), ElementsAre("A", "B", "C"));

        EXPECT_THROW(ConfigItem().ol(), std::exception);
        EXPECT_THROW(ConfigItem(false).ol(), std::exception);
        EXPECT_THROW(ConfigItem(0).ol(), std::exception);
        EXPECT_THROW(ConfigItem(0.1f).ol(), std::exception);
        EXPECT_THROW(ConfigItem(std::string{}).ol(), std::exception);
    }

So after we configure and build the project and then through visual studio we press Test -> Run CTests for Performous all is build correctly, we get a binary (performous_test.exe) in the build directory and we can run it. But it says: No tests were found!!! The test explorer also doesn't show any tests and running ctest in the build directory also shows the same error message: no tests were found!!!

We were wondering what we're doing wrong... as it does work on linux

Edit

For what it's worth... If we remove the dependency of gmock and also remove it from target_link_libraries and make a unittest which doesn't depend on gmock it does get listed and works perfectly fine.

We're using the latest release: 2022.07.25 In VS2022

FrankXie05 commented 2 years ago

This issue is not a vcpkg issue. If you want to enable the testing, please adding include(CTest) to your top-level CMakeLists.txt.

|-- CMakeLists.txt

include(CTest)
enable_testing()
Baklap4 commented 2 years ago

This issue is not a vcpkg issue. If you want to enable the testing, please adding include(CTest) to your top-level CMakeLists.txt.

I've added the include(CTest) before the enable_testing() yet to no avail. The test explorer stays empty and the executeable lists 0/0 tests while there are plenty.

Please also read my Edit part:

For what it's worth... If we remove the dependency of gmock and also remove it from target_link_libraries and make a unittest which doesn't depend on gmock it does get listed and works perfectly fine.

FrankXie05 commented 2 years ago

@Baklap4 Sorry, vcpkg doesn't currently support test cases for ports. This is probably a VS bug https://developercommunity.visualstudio.com/t/no-tests-discovered-for-googletest-and-cmake-anoth/1148799. On the other hand, you can submit an issue with gtest upstream for help. https://github.com/google/googletest. 🙂

FrankXie05 commented 2 years ago

Ping @Baklap4 for response.

FrankXie05 commented 2 years ago

We hope your question was answered to your satisfaction; if it wasn't, you can reopen with more info.