如何使用cmake一次运行所有gtest文件?(How to run all gtest files at once using cmake?)

我有几个包含不同类测试的.cpp文件,如下所示:

#include <gtest/gtest.h> namespace { //lots of tests } int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }

和一个看起来像这样的CMakeLists.txt文件:

add_subdirectory(/usr/src/gtest gtest) include_directories(${GTEST_INCLUDE_DIR}) add_executable(TestA TestA.cpp) target_link_libraries(TestA gtest ) add_executable(TestB TestB.cpp) target_link_libraries(TestB gtest ) add_executable(TestC TestC.cpp) target_link_libraries(TestC gtest )

我喜欢这个设置,因为只运行我正在处理的组件的测试很方便。 执行一个测试文件显然比执行所有测试文件更加强大。 但是,我时不时地想要运行所有测试。 如何在我的设置中轻松实现这一目标?

I have several .cpp files that contain tests for different classes and look like this:

#include <gtest/gtest.h> namespace { //lots of tests } int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }

and a CMakeLists.txt file that looks like this:

add_subdirectory(/usr/src/gtest gtest) include_directories(${GTEST_INCLUDE_DIR}) add_executable(TestA TestA.cpp) target_link_libraries(TestA gtest ) add_executable(TestB TestB.cpp) target_link_libraries(TestB gtest ) add_executable(TestC TestC.cpp) target_link_libraries(TestC gtest )

I like this setup because it's handy to run just the tests for the component I am currently working on. Executing one Test file is obviously much master than executing all of them. However, every now and then I want to run all tests. How can I achieve this easily within my setup?

最满意答案

您可以使用CTest (它通常与CMake一起安装)来实现此目的。

首先,您需要在CMakeLists.txt中include CTest模块 ,然后您只需要为每个测试可执行文件使用add_test命令:

include(CTest)
add_subdirectory(/usr/src/gtest gtest)
include_directories(${GTEST_INCLUDE_DIR})

add_executable(TestA TestA.cpp)
target_link_libraries(TestA gtest)
add_test(NAME AllTestsInA COMMAND TestA)

add_executable(TestB TestB.cpp)
target_link_libraries(TestB gtest)
add_test(NAME AllTestsInB COMMAND TestB)

add_executable(TestC TestC.cpp)
target_link_libraries(TestC gtest)
add_test(NAME AllTestsInC COMMAND TestC)
 

现在,一旦构建了测试版本,就可以从构建文件夹中执行CTest来运行所有测试。 例如,运行Debug测试:

ctest -C Debug
 

或者获得更详细的输出:

ctest -C Debug -V

You can use CTest (it's usually installed along with CMake) to achieve this.

First, you need to include the CTest module in your CMakeLists.txt, then you just need to use the add_test command for each of your test executables:

include(CTest)
add_subdirectory(/usr/src/gtest gtest)
include_directories(${GTEST_INCLUDE_DIR})

add_executable(TestA TestA.cpp)
target_link_libraries(TestA gtest)
add_test(NAME AllTestsInA COMMAND TestA)

add_executable(TestB TestB.cpp)
target_link_libraries(TestB gtest)
add_test(NAME AllTestsInB COMMAND TestB)

add_executable(TestC TestC.cpp)
target_link_libraries(TestC gtest)
add_test(NAME AllTestsInC COMMAND TestC)
 

Now, once your test exes are built, from your build folder you can execute CTest to run all the tests. E.g. to run the Debug tests:

ctest -C Debug
 

or to get more verbose output:

ctest -C Debug -V

                    
                     
          

更多推荐