12

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Eloff
  • 20,828
  • 17
  • 83
  • 112
  • 3
    Rust's tests _are_ standalone executables. You can find them in `target/debug` (or `target/release`). `cargo test` is just a wrapper that lists and execute each of them individually. – mcarton Oct 17 '20 at 12:50
  • 1
    I didn't see it when I went into that directory, but it turns out I missed it because of the name. It was literally in front of my face when I ran the tests: `Running target/debug/deps/mpsc_test-273c365c039107cd` – Eloff Oct 17 '20 at 14:58
  • The question linked as a duplicate is exactly what I was trying to find (and I did a lot of searching, for some reason I could not find that one). – Eloff Nov 05 '20 at 21:48

1 Answers1

10

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
Kirill Lykov
  • 1,293
  • 2
  • 22
  • 39
Ibraheem Ahmed
  • 11,652
  • 2
  • 48
  • 54