How to debug Rust unit tests on Windows?

后端 未结 1 830
遥遥无期
遥遥无期 2020-12-16 15:19

I\'m developing code for the Codingame problems using VS Code on Windows with Rust and the Visual Studio toolchain.

I have found multiple guides explaining how to de

相关标签:
1条回答
  • 2020-12-16 16:01

    Rust unit tests are compiled as separate binaries, which means you debug them exactly the same as any other binary. Once compiled, they are located at ./target/debug/$name-$hash.

    Visual Studio Code

    Here's modified versions of the VS Code configuration files that allow me to debug a unit test.

    tasks.json

    {
        "type": "shell",
        "label": "cargo test build",
        "command": "cargo",
        "args": [
            "test", "--no-run"
        ],
        "problemMatcher": [
            "$rustc"
        ]
    }
    

    launch.json

    {
        "name": "Run Test Debugger",
        "type": "cppvsdbg",
        "request": "launch",
        "program": "${workspaceFolder}/target/debug/buggin-70708b3916187eeb.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "preLaunchTask": "cargo test build",
    }
    

    Working

    Windbg

    Build your tests:

    cargo test --no-run
    

    Open the built executable in Windbg and open the source file.


    Finding the hash is the most annoying aspect. The best solution I know of is to write a small script that builds the tests and then finds the test executable based on which is newest. My Powershell skills are not adequate to the task, nor do I know how to directly integrate this with VS Code or Windbg.

    There are open issues for Cargo to help with identifying the file:

    • produce deterministic filenames for build --test and test --no-run (#1924)
    • Make it possible to run binaries produced by cargo directly (#3670)
    0 讨论(0)
提交回复
热议问题