40

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?

5
  • @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
    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
    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
    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
    Apr 23, 2015 at 14:41
  • 1
    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
    Dec 9, 2018 at 6:32

1 Answer 1

43

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

Your Answer

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

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