简体   繁体   中英

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

Since CLion 2020.3 EAP CTest is supported from the box .

There is currently no CTest support in CLion.

The feature request is here .

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: 在此处输入图像描述

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

Since version CLion 2020.3 the CTest is supported from the box for CMake 3.14 and above .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM