如何构建独立的测试二进制文件以在调试器下运行?

Elo*_*off 5 rust rust-cargo

我想将一个特定的单元测试打包成一个可执行的二进制文件,我可以在调试器下运行它。

我怎样才能用货物做到这一点?

Ibr*_*med 13

运行cargo test结果如下:

\n
\xe2\x9d\xaf cargo test\n   Compiling mycrate v0.1.0 (mycrate)\n    Finished test [unoptimized + debuginfo] target(s) in 0.44s\n     Running target/debug/deps/mycrate-1a3af12dafb80133\n\nrunning tests...\n
Run Code Online (Sandbox Code Playgroud)\n

正如你可以通过这一行看到:

\n
Running target/debug/deps/mycrate-1a3af12dafb80133\n
Run Code Online (Sandbox Code Playgroud)\n

该测试实际上一个独立的可执行文件,位于target/debug/deps/.

\n

您可以直接运行可执行文件:

\n
\xe2\x9d\xaf ./target/debug/deps/mycrate-1a3af12dafb80133\n\nrunning tests...\n
Run Code Online (Sandbox Code Playgroud)\n

您还可以构建可执行文件,而无需使用以下标志运行测试no-run

\n
\xe2\x9d\xaf cargo test --no-run\n   Compiling mycrate v0.1.0 (mycrate)\n    Finished test [unoptimized + debuginfo] target(s) in 0.41s\n
Run Code Online (Sandbox Code Playgroud)\n

  • `cargo build` 现在有一个参数 `--test [<name>]`,它使用指定的测试集构建测试二进制文件。 (2认同)