46

I'm using cargo build --release to build my project in release configuration and cargo test to build and run my tests.

However, I'd like to also build my tests in release mode; can this be done using cargo?

6
  • @ViktorDahl Thanks, but it's not really what I'm after: I was hoping to be able to change the configuration with which tests are built via some command line arg passed to cargo.
    – Fraser
    Commented Apr 23, 2015 at 11:54
  • To satisfy my own curiosity, why do you want to do this? The main reason I can think is for running profiling tests, but those are already built with optimization.
    – Shepmaster
    Commented Apr 23, 2015 at 12:22
  • 2
    I'm used to C++ and found that some issues were only reproducible using release code. They were always horrible to debug, but running tests in release (or preferably RelWIthDebInfo) was often invaluable. I don't have a need yet, but I'm anticipating one :)
    – Fraser
    Commented Apr 23, 2015 at 12:29
  • @rubenvb I'll take the bait :) I disagree - often the issues were down to timing (things running significantly faster in release obviously) or code within #ifdef NDEBUG blocks containing bugs.
    – Fraser
    Commented Apr 23, 2015 at 14:41
  • 2
    optimized code can have problems because of CPU bugs, OS bugs, library bugs, LLVM bugs, assembler bugs, linker bugs, etc etc. the code that goes out to customers should be the same code that passes test.
    – don bright
    Commented Dec 9, 2018 at 6:32

2 Answers 2

49

cargo test --release exists, but it is slightly different than just enabling optimizations. For example, debug assertions become disabled.

You can also set opt-level in the [profile.test] section of your Cargo.toml, as Viktor Dahl suggests.

0
2

the closest settings on Cargo.toml to cargo test --release are to append this to the Cargo.toml file:

[profile.test]
inherits = "release"

Then you use cargo test and cargo will do the --release for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.