繁体   English   中英

用拍手将参数传递给货物测试

[英]Passing arguments to cargo test with clap

该程序采用配置文件的路径。 例如cargo run -- -c path/to/yaml 然而,这不适用于货物测试。 cargo test -- -c path/to/yaml会出现以下错误: error: Unrecognized option: 'c'

尝试和研究

Clap 提供了一个方法fn from_args() -> Self ,但不完全知道这将如何解决问题。 通过使其成为集成测试并添加解决了类似的问题

[[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

到 cargo.toml 文件。
就我而言,我想测试一些功能,从而进行单元测试。 我不相信这会奏效。

我认为最简单的方法是让fn main_body(args: Args)完成真正的工作,然后通过直接在源代码中而不是在命令行中传递 args 来测试main_body

use clap::Parser; // 3.1.18

#[derive(Parser)]
struct Args {
    #[clap(short, long)]
    name: String,
}

fn main_body(args: Args) -> Result<(), ()> {
    // Your main body here
    Ok(())
}

fn main() -> Result<(), ()> {
    let args = Args::parse();
    main_body(args)
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test() {
        let args = Args { name: "Bob".into() };
        assert_eq!(main_body(args), Ok(()));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM