如何在Windows上调试Rust单元测试? [英] How to debug Rust unit tests on Windows?
问题描述
我正在使用带有Rust和Visual Studio工具链的Windows上的VS Code开发针对Codingame问题的代码。
我发现了多本指南,解释了如何调试货物建造
生成的可执行文件,最好的是
< h2> Windbg
构建测试:
cargo test --no-run
在Windbg中打开生成的可执行文件并打开源文件。
查找哈希是最令人讨厌的方面。我所知道的最好的解决方案是编写一个小的脚本来构建测试,然后根据最新的脚本找到测试可执行文件。我的Powershell技能不足以完成这项任务,我也不知道如何将其直接与VS Code或Windbg集成。
Cargo有很多开放性问题可以帮助您确定文件:
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 debug the executable generated by cargo build
, the best being Debug Rust on Windows with Visual Studio Code and the MSVC Debugger.
However, when I face problems, I tend to write unit tests (I've done that in Java, JavaScript, Ruby, ...), which I then debug. Unfortunately, I can't find any way to do that in Rust. How do I configure my environment to debug my tests?
I'm not talking about adding println!
statements in my tests, as I already know how to do that. I'm also not talking about adding new assertions, because those reside in the test, not in the tested code.
What I want is to use the VS Code Debugger on the code called by my test.
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
andtest --no-run
(#1924) - Make it possible to run binaries produced by cargo directly (#3670)
这篇关于如何在Windows上调试Rust单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!