Search code examples
c++cmakevisual-studio-2019googletest

CMake and GTest in VS 2019 test building failure


Somehow I have the same problem I had last time see here and I can't solve it this time. I have my CMakeLists.txt file:

cmake_minimum_required (VERSION 3.20)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_TOOLCHAIN_FILE "C:/Users/JackOfShadows/vcpkg/scripts/buildsystems/vcpkg.cmake")

project ("CMakeProject1")

find_package(GTest CONFIG REQUIRED)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

add_executable (CMakeProject1 "main.cpp" TestClass.cpp TestClass.h)
target_link_libraries (CMakeProject1
    PRIVATE GTest::gmock GTest::gtest GTest::gmock_main GTest::gtest_main
)

enable_testing()
find_package(GTest CONFIG REQUIRED)
add_executable(SomeTests
    "AppTests.cpp" TestClass.cpp TestClass.h
)

target_link_libraries (SomeTests
    PRIVATE GTest::gmock GTest::gtest GTest::gmock_main GTest::gtest_main
)

include (GoogleTest)
gtest_discover_tests(SomeTests)

It builds and links as it's supposed to. But no tests are discovered in AppTests.cpp:

#include <gtest/gtest.h>

namespace SomeNamespaceTests {
    // Demonstrate some basic assertions.

    TEST(HelloTest, BasicAssertions) {

        // Expect two strings not to be equal.
        EXPECT_STRNE("hello", "world");
        // Expect equality.
        EXPECT_EQ(7 * 6, 42);
    }
}

TestResult


Solution

  • Per suggerstion by @local_ninja I've linked project only to gtest_main and now tests are running okay.