将自定义命令行参数传递给Rust测试 我有一个RoST测试,它被委托给C++测试套件,并希望将命令行参数传递给它。我的第一次尝试是 // in mod ffi pub fn run_tests(cli_args: &mut [String]) -> bool; #[test] fn run_cpp_test_suite() { let mut cli_args: Vec<String> = env::args().collect(); if !ffi::run_tests( cli_args.as_mut_slice(), ) { panic!("C++ test suite reported errors"); } }
我预料将自定义命令行参数传递给Rust测试 我有一个RoST测试,它被委托给C++测试套件,并希望将命令行参数传递给它。我的第一次尝试是 // in mod ffi pub fn run_tests(cli_args: &mut [String]) -> bool; #[test] fn run_cpp_test_suite() { let mut cli_args: Vec<String> = env::args().collect(); if !ffi::run_tests( cli_args.as_mut_slice(), ) { panic!("C++ test suite reported errors"); } },c++,rust,command-line-arguments,C++,Rust,Command Line Arguments,我预料 cargo test -- --test-case="X" 让运行\u cpp\u test\u套件访问并传递--test case=“X”参数。但事实并非如此;我得到错误:无法识别的选项:'testcase'和货物测试--help显示它有一组固定的选项 Usage: --help [OPTIONS] [FILTER] Options: --include-ignored Run ignored a
cargo test -- --test-case="X"
让运行\u cpp\u test\u套件
访问并传递--test case=“X”
参数。但事实并非如此;我得到错误:无法识别的选项:'testcase'
和货物测试--help
显示它有一组固定的选项
Usage: --help [OPTIONS] [FILTER]
Options:
--include-ignored
Run ignored and not ignored tests
--ignored Run only ignored tests
...
我的另一个想法是在环境变量中传递参数,即
DOCTEST_ARGS="--test-case='X'" cargo test
<>但是我需要把这个字符串分解成参数(至少在正确的空间和引号中),或者在锈中或者C++中。 涉及两个锈链链。
cargo-test
本身在包或工作区中查找所有可测试目标,使用cfg(test)
构建它们,并运行这些二进制文件<代码>货物测试处理--
左侧的参数,右侧的参数传递给二进制文件
那么
测试是使用--test
选项构建的,该选项用于rustc
,该选项创建一个带有main
函数的可执行文件,该函数在多个线程中自动运行带有#[test]属性注释的所有函数<代码>#[bench]带注释的函数也将在一次迭代中运行,以验证它们是否正常工作
通过在目标清单设置中设置harness=false
,可以禁用libtest线束,在这种情况下,您的代码需要提供自己的main
功能来处理正在运行的测试
“libtest工具”是拒绝额外参数的工具。在您的情况下,由于您打算运行整个其他测试套件,我认为禁用线束是合适的
tests/
:
Cargo.toml
src/
lib.rs
...
tests/
cpp_test.rs
Cargo.toml
中为其编写一个明确的代码:
[[test]]
name = "cpp_test"
# path = "tests/cpp_test.rs" # This is automatic; you can use a different path if you really want to.
harness = false
let args = var ("DOCTEST_ARGS").unwrap_or_else (|_| String::new());
let args = shell_words::split (&args).expect ("failed to parse DOCTEST_ARGS");
Command::new ("cpptest")
.args (args)
.spawn()
.expect ("failed to start subprocess")
.wait()
.expect ("failed to wait for subprocess");
谢谢,说实话,我原以为这是不可能的!明天我应该有时间检查一下(然后接受答案)。
let args = var ("DOCTEST_ARGS").unwrap_or_else (|_| String::new());
let args = shell_words::split (&args).expect ("failed to parse DOCTEST_ARGS");
Command::new ("cpptest")
.args (args)
.spawn()
.expect ("failed to start subprocess")
.wait()
.expect ("failed to wait for subprocess");