CLion:在构建之前运行测试

问题描述 投票:1回答:1

我在CLion(C ++ + CMake)中创建了一个项目,其中有一个具有2种配置的shared library项目Debug |发布。我还为单元测试实现了google测试。

当配置为发布时,我想在构建之前运行一些测试(或全部)。如果测试失败,则应该not构建库。

这可能吗?如果可以,怎么办?

cmake automated-tests googletest clion jetbrains-ide
1个回答
0
投票

我找到了add_custom_command()的答案。在我的主要CMakeLists.txt中,我有

if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
   #Rebuild the tests just in case some tests has changed and the executable was not rebuild
   add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
           COMMAND ${CMAKE_COMMAND} --build "${CMAKE_BINARY_DIR}" --target tests --
           )

   if(${WIN32})
      set(TESTS_BIN tests.exe)
   else()
      set(TESTS_BIN tests)
   endif()
   #Run the tests executable
   add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
           COMMAND "${CMAKE_BINARY_DIR}/${TESTS_BIN}"
           )
endif()

add_custom_command()很聪明,因此当tests可执行文件未返回0(所有测试均已成功通过)时,构建将失败,并且不会构建库。

© www.soinside.com 2019 - 2024. All rights reserved.