Home > Net >  Unable to provide CLI arguments to `cargo test`
Unable to provide CLI arguments to `cargo test`

Time:07-23

I used to be able to run specific, named tests from the command-line interface like this: 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.

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 is decorated with #[derive(StructOpt)].

CodePudding user response:

The problem was that arguments intended for cargo test were interpreted as arguments for the application.

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.

  • Related