How can I build standalone test binary to run under a debugger?

Viewed 2756

I want to package up a specific unit test into an executable binary that I can run myself under a debugger.

How can I do this with cargo?

1 Answers

Running cargo test results in the following output:

❯ cargo test
   Compiling mycrate v0.1.0 (mycrate)
    Finished test [unoptimized + debuginfo] target(s) in 0.44s
     Running target/debug/deps/mycrate-1a3af12dafb80133

running tests...

As you can see by this line:

Running target/debug/deps/mycrate-1a3af12dafb80133

The test actually is a standalone executable, located in target/debug/deps/.

You can run the executable directly:

❯ ./target/debug/deps/mycrate-1a3af12dafb80133

running tests...

You can also build the executable without running the tests with the no-run flag:

❯ cargo test --no-run
   Compiling mycrate v0.1.0 (mycrate)
    Finished test [unoptimized + debuginfo] target(s) in 0.41s
Related