如何在VSCode中配置带有自定义参数的货物测试



我有一些创建/读/写/删除文件的测试,我总是在每个测试中使用相同的文件名,因此我需要按顺序运行它们,以避免在同一文件上同时操作。在命令行中,我可以这样做

cargo test -- --test-threads=1

然而,在VSCode运行/调试菜单,它似乎不工作。我使用的是Rust Analyzer自动生成的配置,加上顺序运行的这些额外参数。

这是我的launch.json:

{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'my-project'",
"cargo": {
"args": [
"build",
"--bin=my-project",
"--package=my-project"
],
"filter": {
"name": "my-project",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'my-project'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin my-project",
"--package my-project",
"--", // here I've added the arguments
"--test-threads=1", // here I've added the arguments
],
"filter": {
"name": "my-project",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

这是我运行第二个命令(调用cargo test的命令)时得到的输出:

Running `cargo test --no-run --bin my-project --package my-project --message-format=json -- --test-threads=1`...
error: Found argument '--bin my-project' which wasn't expected, or isn't valid in this context
Did you mean '--bin'?
If you tried to supply `--bin my-project` as a value rather than a flag, use `-- --bin my-project`
USAGE:
cargo.exe test --no-run --bin [<NAME>]
For more information try --help

VSCode使用了两个步骤:

  1. 调用cargo test --no-run编译测试可执行文件和
  2. 直接调用测试可执行文件。

你应该把--test-threads=1放在最后一个args数组中:

{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'my-project'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=my-project",
"--package=my-project",
],
"filter": {
"name": "my-project",
"kind": "bin"
}
},
"args": [ "--test-threads=1" ],
"cwd": "${workspaceFolder}"
}

相关内容

  • 没有找到相关文章

最新更新