c++cmakegoogletestclionctest

Running Tests in CLion using CMake's CTest


I have a C++ project using CMake. The project is built on CentOS machine. I have configured CLion to build remotely from MacOS. I have unit tests for the project and I am trying to run them from CLion. I can run the tests from CentOS machine using CTest like below

ctest -r utCppProject -v

CLion tries to run the executable directly with gtest flags like below

./utCppProject --gtest_filter=* --gtest_color=no
Process finished with exit code 0

No tests are actually run.

How can I configure CLion to be able to use CTest for running unit tests?

Here is my CMakeLists.txt for the unit test project

cmake_minimum_required(VERSION 3.4.1)

include(../../cmake-dependencies/Boost.cmake)
include(../../cmake-dependencies/GoogleTest.cmake)

set(CMAKE_BINARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../build")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)


include_directories(${GOOGLE_TEST_DIR}/googletest/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/.)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)


set(TARGET utCppProject)

add_executable (
   ${TARGET}
   utCppProject.cpp
)

target_link_libraries (
    ${TARGET}
    CppProject
    gtest
    boost_system
    pthread
)

set(CMAKE_CXX_FLAGS "-fPIC -DPIC -Wall -Werror -std=c++0x")

set(TEST_OUTPUT "${CMAKE_BINARY_DIR}/test_results/${TARGET}.xml")
add_test(${TARGET} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET})
set_tests_properties(${TARGET} PROPERTIES
  ENVIRONMENT 
  "UT_FOLDER_PATH=${CMAKE_CURRENT_SOURCE_DIR};GTEST_OUTPUT=xml:${TEST_OUTPUT}")

Solution

  • You can easily configure CLion to use CTest. Simply duplicate the default configuration for the test target and configure it to use the CTest executable and set the working directory to the build directory: enter image description here

    In detail: