38

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • 6
    I'm not entirely sure it's what you're asking for, but you can use [profiles](http://doc.crates.io/manifest.html#the-%5Bprofile.%2A%5D-sections) to control how tests are built. `[profile.test] opt-level = 3` would enable full optimizations for your tests. – Viktor Dahl Apr 23 '15 at 11:12
  • @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 '15 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 '15 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 '15 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 '15 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 09 '18 at 06:32

1 Answers1

41

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
huon
  • 94,605
  • 21
  • 231
  • 225