Some test in a project fail in release mode cargo test --release
because there are debug_asserts that are expected to panic for those tests. Is there a way to keep debug_asserts on but use the release profile when running tests?
Some test in a project fail in release mode cargo test --release
because there are debug_asserts that are expected to panic for those tests. Is there a way to keep debug_asserts on but use the release profile when running tests?
1 Answer
Reset to default 5If your code should panic in --release
mode, then use a normal assert!
instead of a debug_assert!
.
If your code shouldn't panic in --release
mode but don't want cargo test --release
to report a failure, then you can ignore the test:
#[test]
#[cfg_attr(not(debug_assertions), ignore)]
fn my_test() { ... }
If you really want to keep debug_assert!
for testing while getting other effects from --release
, then you enable the configuration via rustflags from an environment variable (or other config):
RUSTFLAGS="-C debug-assertions" cargo test --release
Or you can modify the release profile or create a custom profile:
# Cargo.toml
[profile.release]
debug-assertions = true
# or
[profile.dev-optimized]
inherits = "dev"
opt-level = 3
cargo test --profile=dev-optimized