我正在使用 CTest 并希望在运行时将命令行参数传递给基础测试。我知道有一些方法可以将命令行参数硬编码到 CMake/CTest 脚本中,但我想在运行时指定命令行参数,并使这些参数通过 CTest 传递到基础测试。
这个能做到吗?
我正在使用 CTest 并希望在运行时将命令行参数传递给基础测试。我知道有一些方法可以将命令行参数硬编码到 CMake/CTest 脚本中,但我想在运行时指定命令行参数,并使这些参数通过 CTest 传递到基础测试。
这个能做到吗?
if(NOT DEFINED ENV{TESTS_ARGUMENTS})
set(ENV{TESTS_ARGUMENTS} "--default-arguments")
endif()
execute_process(COMMAND ${TEST_EXECUTABLE} $ENV{TESTS_ARGUMENTS} RESULT_VARIABLE result)
if(NOT "${result}" STREQUAL "0")
message(FATAL_ERROR "Test failed with return value '${result}'")
endif()
然后,当您添加测试时,请使用
add_test(
NAME MyTest
COMMAND ${CMAKE_COMMAND} -DTEST_EXECUTABLE=$<TARGET_FILE:MyTest> -P ${CMAKE_SOURCE_DIR}/cmake/RunTests.cmake
)
最后,您可以使用自定义参数运行测试,方法如下:
cmake -E env TESTS_ARGUMENTS="--custom-arguments" ctest
TESTS_ARGUMENTS="--custom-arguments" ctest
WILL_FAIL
属性。当然,我希望它能像调用ctest -- --custom-arguments
那样简单,但正如Stones所说,你不能总是得到你想要的。我不确定我完全理解你想要什么,但我仍然可以给你一种在运行时将参数传递给CTest测试的方法。
我将用CTK(通用工具包,https://github.com/commontk/CTK)为例:
在构建目录中(例如:CTK-build / CTK-build,它是一个超级构建),如果我运行:(“-V”表示详细模式,“-N”表示仅查看模式)
ctest -R ctkVTKDataSetArrayComboBoxTest1 -V -N
UpdateCTestConfiguration from : /CTK-build/CTK-build/DartConfiguration.tcl
Parse Config file:/CTK-build/CTK-build/DartConfiguration.tcl
Add coverage exclude regular expressions.
Add coverage exclude: /CMakeFiles/CMakeTmp/
Add coverage exclude: .*/moc_.*
Add coverage exclude: .*/ui_.*
Add coverage exclude: .*/Testing/.*
Add coverage exclude: .*/CMakeExternals/.*
Add coverage exclude: ./ctkPixmapIconEngine.*
Add coverage exclude: ./ctkIconEngine.*
UpdateCTestConfiguration from :/CTK-build/CTK-build/DartConfiguration.tcl
Parse Config file:/CTK-build/CTK-build/DartConfiguration.tcl
Test project /CTK-build/CTK-build
Constructing a list of tests
Done constructing a list of tests
178: Test command: /CTK-build/CTK-build/bin/CTKVisualizationVTKWidgetsCppTests "ctkVTKDataSetArrayComboBoxTest1"
Labels: CTKVisualizationVTKWidgets
Test #178: ctkVTKDataSetArrayComboBoxTest1
Total Tests: 1
您可以将“测试命令”复制粘贴到终端中:
/CTK-build/CTK-build/bin/CTKVisualizationVTKWidgetsCppTests "ctkVTKDataSetArrayComboBoxTest1"
并添加参数,例如用于交互式测试的“-I”:
/CTK-build/CTK-build/bin/CTKVisualizationVTKWidgetsCppTests "ctkVTKDataSetArrayComboBoxTest1" "-I"
Matthieu的回答给了我启示,让我成功地解决了问题。
对于我的代码,我做了以下操作:
输入命令ctest -V -R TestMembraneCellCrypt -N
以获取输出:
...
488: Test command: path/to/ctest/executable/TestMembraneCellCrypt
Labels: Continuous_project_ChasteMembrane
Test #488: TestMembraneCellCrypt
...
然后我复制了测试命令
并在那里提供了参数:
path/to/ctest/executable/TestMembraneCellCrypt -e 2 -em 5 -ct 10
我要注意一下,我正在使用的软件包(Chaste)非常庞大,所以可能会发生我不知道的事情。
a=${VAR:-20}
如果VAR已设置,shell将为"a"分配VAR的值,否则将为其分配默认值20 - 这也可以是表达式的结果。
带有CMake代码的片段如下:
find_program(SHELL sh)
string(JOIN " " LIBFUZZER_OPTS
-mutate_depth=20
-runs=$\{RUNS:-${DEFAULT_RUNS_NUMBER}\}
-artifact_prefix=${test_name}_
)
add_test(NAME ${test_name}
COMMAND ${SHELL} -c "$<TARGET_FILE:${test_name}> ${LIBFUZZER_OPTS}"
)
$ RUNS=10 ctest -R luaL_gsub_test --verbose
<snipped>
1: Done 10 runs in 0 second(s)
$ ctest -R luaL_gsub_test --verbose
<snipped>
1: Done 5 runs in 0 second(s)