skip to Main Content

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}")

4

Answers


  1. There is currently no CTest support in CLion.

    The feature request is here.

    Login or Signup to reply.
  2. 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:

    • Executable > Select other… > Find and select ctest (for me it is /usr/bin/ctest, on UNIX-like systems you can use which ctest to find it)
    • Set program arguments – -j sets the amount of threads to use, then the name of the executable to test, and --output-on-failure to get the test output when something went wrong – you could simply set it to -r utCppProject -v
    • Set working directory to cmake-build-debug under the project dir, the default build directory for CLion
    Login or Signup to reply.
  3. Since CLion 2020.3 EAP CTest is supported from the box.

    Login or Signup to reply.
  4. Since version CLion 2020.3 the CTest is supported from the box for CMake 3.14 and above.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search