Anna S
Anna S

Reputation: 3

How can I run gtest in C++ code using CMake? (tests not seen)

I'm working on Windows using CLion on my university C++ project and tried to add some example tests code. I have included latest Google Test framework from GitHub. I have got separate directory for source and tests. The problem is that tests are not seen by compiler. I get info "Empty test suite.", although main function is properly invoked.

My directory looks like follows:

- root
- src
- tests
    * tests/components/ColorTest.cpp
    * gtest.cpp
    * CMakeLists.txt
- CMakeLists.txt

Below is the code:

CMakeLists.txt

cmake_minimum_required(VERSION 3.7)
project(SI_lab_2)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")

include_directories(src)
include_directories(tests)

add_subdirectory(src)
add_subdirectory(tests)

tests/CMakeLists.txt

cmake_minimum_required(VERSION 3.7)
project(tests)

add_subdirectory(lib/googletest)

enable_testing()

include_directories(${gtest_SOURCE_DIR_}/include ${gtest_SOURCE_DIR_})

set(SOURCE_FILES
        gtest.cpp
        tests/components/ColorTests.cpp)

add_executable(tests_run gtest.cpp)

target_link_libraries(tests_run gtest gtest_main)

add_test(eq tests_run)

tests/tests/components/ColorTests.cpp

#include <gtest/gtest.h>

TEST(ColorTests, eq)
{
    EXPECT_EQ(1, 0);
}

TEST(ColorTests, noteq)
{
    EXPECT_NE(1, 0);
}

tests/gtest.cpp

#include <gtest/gtest.h>

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

Hope you can help!

Upvotes: 0

Views: 1788

Answers (1)

guenni_90
guenni_90

Reputation: 541

In your tests/CMakeLists.txt you should substitude add_executable(tests_run gtest.cpp) by add_executable(tests_run ${SOURCE_FILES}). The test you wrote is not getting compiled as you forgot to add it to add_executable

Upvotes: 2

Related Questions