最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - How to `cargo test --release` with debug_asserts on - Stack Overflow

programmeradmin5浏览0评论

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?

Share Improve this question edited Mar 16 at 20:33 Finomnis 23k2 gold badges31 silver badges36 bronze badges asked Mar 16 at 19:45 A. K.A. K. 38.4k17 gold badges56 silver badges102 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

If 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
发布评论

评论列表(0)

  1. 暂无评论