[英]How do I pass in parameters to change the output of unit tests when using CMake and BOOST?
In a previous question , I asked on how to get unit tests with CMake
and BOOST
to run.在上一个问题中,我询问了如何使用
CMake
和BOOST
运行单元测试。 In this question, I need to format the output of the test runs.在这个问题中,我需要格式化测试运行的输出。 In particular, the default output is not very insightful and I want to change it;
特别是默认输出不是很有见地,我想改变它; I need to pass in parameters like
--report-level=detailed
when running the tests.在运行测试时,我需要传入
--report-level=detailed
类的参数。
My tests/CMakeLists.txt
file looks like the following.我的
tests/CMakeLists.txt
文件如下所示。
find_package (Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
add_definitions (-DBOOST_TEST_DYN_LINK)
include_directories (../include ${Boost_INCLUDE_DIRS})
file(GLOB TEST_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
foreach(testSrc ${TEST_SRCS})
get_filename_component(testName ${testSrc} NAME_WE)
add_executable(${testName} ${testSrc})
target_link_libraries(${testName} ${Boost_LIBRARIES} my_lib)
set_target_properties(${testName} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/testBin)
add_test(NAME ${testName}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/testBin
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/testBin/${testName}
)
endforeach(testSrc)
I have modified it to look as follows.我已将其修改为如下所示。 Note I have tried to pass in the arguments in multiple places out of desperation, one during
add_test
and one during add_custom_command
.注意我曾尝试在参数传递在多个地方出于绝望,一个在
add_test
,并在一个add_custom_command
。
find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
add_definitions(-DBOOST_TEST_DYN_LINK)
include_directories(../include/graph ${Boost_INCLUDE_DIRS})
file(GLOB TEST_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
foreach (testSrc ${TEST_SRCS})
get_filename_component(testName ${testSrc} NAME_WE)
add_executable(${testName} ${testSrc})
target_link_libraries(${testName} ${Boost_LIBRARIES} my_lib)
set_target_properties(${testName} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/testBin)
add_test(NAME ${testName}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/testBin
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/testBin/${testName} --build_info --output_format=XML --log_level=all --report_level=detailed
)
add_custom_command(
OUTPUT ${testName}
POST_BUILD
COMMAND ${testName} ${CMAKE_CURRENT_BINARY_DIR}/testBin/${testName}
ARGS "--build_info --output_format=XML --log_level=all --report_level=detailed"
)
endforeach (testSrc)
Here's a modified example of the output when running make test
.这是运行
make test
时输出的修改示例。
Running tests...
Test project /mnt/c/Users/me/git/my_lib/cmake-build-debug
Start 1: test_A
1/7 Test #1: test_A ..................... Passed 0.02 sec
Start 2: test_B
2/7 Test #2: test_B ......................... Passed 0.02 sec
Start 3: test_C
3/7 Test #3: test_C ........................ Passed 0.03 sec
Start 4: test_D
4/7 Test #4: test_D ....................... Passed 0.03 sec
Start 5: test_E
5/7 Test #5: test_E ........................ Passed 0.02 sec
Start 6: test_F
6/7 Test #6: test_F .................. Passed 0.02 sec
Start 7: test_G
7/7 Test #7: test_G .................... Passed 0.02 sec
100% tests passed, 0 tests failed out of 7
Total Test time (real) = 0.17 sec
I actually want to experiment with flags to get the output to be much more verbose.我实际上想尝试使用标志来使输出更加详细。 For example
../tests/testBin/test_A --report_level=detailed
.例如
../tests/testBin/test_A --report_level=detailed
。
Running 2 test cases...
Test module "TestModule" has passed with:
2 test cases out of 2 passed
39 assertions out of 39 passed
Test suite "test_x" has passed with:
2 test cases out of 2 passed
39 assertions out of 39 passed
Test case "test_x/y" has passed with:
34 assertions out of 34 passed
Test case "test_x/z" has passed with:
5 assertions out of 5 passed
While searching on the internet, some of the top results pass in the flags during add_test
( here ) and others during add_custom_command
(and here ).在 Internet 上搜索时,一些顶级结果在
add_test
( 此处)和add_custom_command
(和此处)期间传递标志。 I am not sure how they get it to work.我不确定他们是如何让它发挥作用的。 Here's some information on my environment.
这是有关我的环境的一些信息。
Any ideas on how/where to pass in these flags/arguments to change the output of the tests in a CMakeLists.txt
file?关于如何/在何处传递这些标志/参数以更改
CMakeLists.txt
文件中的测试输出的任何想法?
I think what you are looking for might be CTest which provides a Build And Test Mode .我认为您正在寻找的可能是CTest ,它提供了Build And Test Mode 。 With this, you should be able to invoke your test command with
有了这个,你应该能够调用你的测试命令
ctest --build-and-test <path-to-source> <path-to-build>
--build-generator <generator>
[<options>...]
[--build-options <opts>...]
[--test-command <command> [<args>...]]
Make sure to add these two commands in your CMakeLists.txt确保在 CMakeLists.txt 中添加这两个命令
enable_testing()
include(CTest)
With this, you should be able to remove the custom command, and invoke your tests directly using the CTest command.有了这个,您应该能够删除自定义命令,并使用 CTest 命令直接调用您的测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.