In my projects, I install tests in a separate module, as for tests. Then I create a Cargo function that allows them. In this passage, I used the function name unstable , but you can use whatever you want:
Cargo.toml
# ... [features] unstable = []
Src / lib.rs
#![cfg_attr(feature = "unstable", feature(test))] #[cfg(test)] mod tests { #[test] fn a_test() { assert_eq!(1, 1); } } #[cfg(all(feature = "unstable", test))] mod bench { extern crate test; use self::test::Bencher; #[bench] fn a_bench(b: &mut Bencher) { let z = b.iter(|| { test::black_box(|| { 1 + 1 }) }); } }
The line #[cfg(all(feature = "unstable", test))] says only to compile the next element if the function is installed and we still compile in test mode. Similarly, #![cfg_attr(feature = "unstable", feature(test))] only includes the flag of the test function when the unstable function is enabled.
Here is an example in the wild .
Shepmaster
source share