我有一个Rust测试,使用doctest将其委托给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");
}
}
因为cargo test help
显示
USAGE:
cargo.exe test [OPTIONS] [TESTNAME] [-- <args>...]
我期望
cargo test -- --test-case="X"
让
run_cpp_test_suite
能够使用并传递 --test-case="X"
参数,但实际上并没有起作用,我收到了 error: Unrecognized option: 'test-case'
的错误信息,并且 cargo test -- --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
但是我需要以某种方式将该字符串分割成参数(至少正确处理空格和引号),可以在Rust或C++中完成。