Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>. 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)].

like image 322
Thorkil Værge Avatar asked Oct 23 '25 15:10

Thorkil Værge


1 Answers

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.

like image 109
Thorkil Værge Avatar answered Oct 26 '25 05:10

Thorkil Værge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!