简体   繁体   English

无法为“货物测试”提供 CLI 参数

[英]Unable to provide CLI arguments to `cargo test`

I used to be able to run specific, named tests from the command-line interface like this: cargo test <test_name> .我曾经能够从命令行界面运行特定的命名测试,如下所示: cargo test <test_name> But now this gives me the error但是现在这给了我错误

running 1 test
error: Found argument '<test_name>' which wasn't expected, or isn't valid in this context

Other arguments to cargo test also don't work. cargo test的其他论据也不起作用。

The line that causes the error is this line in the test setup:导致错误的行是测试设置中的这一行:

let cli_default_args = Arc::new(cli_args::Args::from_args());

Where the cli_args::Args struct is a struct that holds the value of the command line arguments and the from_args function comes from the StructOpt package derivation.其中cli_args::Args结构是保存命令行参数值的结构,而from_args函数来自StructOpt包派生。 cli_args::Args is decorated with #[derive(StructOpt)] . cli_args::Args#[derive(StructOpt)]装饰。

The problem was that arguments intended for cargo test were interpreted as arguments for the application.问题是用于cargo test的参数被解释为应用程序的参数。

Replacing the problematic line in the test setup在测试设置中替换有问题的行

let cli_default_args = Arc::new(cli_args::Args::from_args());

with

let cli_default_args = Arc::new(cli_args::Args::from_iter::<Vec<String>>(vec![]));

fixes the problem.解决问题。 The above code means that your test setup runs as if the program didn't get any CLI arguments, everything is running with its default values.上面的代码意味着您的测试设置就像程序没有获得任何 CLI 参数一样运行,一切都以其默认值运行。

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

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