Running Tests Conditionally in Rust

November 11, 2022

In Rust it is possible to run tests conditionally.

Feature-gated test:

#[test]
#[cfg(feature = "add-a-variant")]
fn add_a_variant() {...}

It will run only if the following parameters are used:

cargo test --features add-a-variant

This is useful when doing TDD and if the code which will pass the test is not ready yet. We can mark the test as pending until the feature is ready.


Profile picture

Things I've learned, collected in luisjuarros/til.
You may also enjoy my blog and/or my twitter.