Search code examples
unit-testingvisual-studio-coderustvscode-debugger

How to configure cargo test with custom arguments in VSCode


I have some tests which create/read/write/delete a file and I always use the same file name in each of them, therefore I need to run them sequentially to avoid simultaneous operations on the same file. In the command line I can just do it like this

cargo test -- --test-threads=1

however in the VSCode Run/Debug menu it doesn't seem to work. I'm using the autogenerated configuration with Rust Analyzer, plus these extra arguments for the sequential run.

This is my 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}"
        }
    ]
}

And this is the output I get when running the second command (the one which calls 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

Solution

  • VSCode uses a two steps process:

    1. it calls cargo test --no-run to compile the test executable and
    2. it calls the test executable directly.

    You should put --test-threads=1 in the last args array:

    {
        "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}"
    }