将自定义命令行参数传递给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
    
  • <>代码> CppyTest.RS,而不是用<代码> > [Test] < /Cuff>属性编写函数,编写一个正常的<代码>主< <代码>函数,读取<代码> Env::ARSs*()/Cuff>并调用C++测试。 [免责声明:我熟悉这些机制,因为我使用了标准基准测试(同样需要禁用默认工具),但实际上我还没有按照您所寻找的方式编写带有自定义参数的测试。因此,一些细节可能是错误的。如果有什么需要更正的,请告诉我。]

    此外,如果您不想编写自己的测试工具,可以使用以下shell规则将环境变量拆分为单个参数:

    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");