I’m working on a Rust library that provides access to some hardware devices. There are two device types, 1 and 2, and the functionality for type 2 is a superset of the functionality for type 1.
I want to provide different test suites for different circumstances:
- tests with no connected device (basic sanity checks, e. g. for CI servers)
- tests for the shared functionality (requires a device of type 1 or 2)
- tests for the type 2 exclusive functionality (requires a device of type 2)
I’m using features to represent this behavior: a default feature test-no-device
and optional features test-type-one
and test-type-two
. Then I use the cfg_attr
attribute to ignore the tests based on the selected features:
#[test]
#[cfg_attr(not(feature = "test-type-two"), ignore)]
fn test_exclusive() {
// ...
}
#[test]
#[cfg_attr(not(any(feature = "test-type-two", feature = "test-type-one")), ignore)]
fn test_shared() {
// ...
}
This is rather cumbersome as I have to duplicate this condition for every test and the conditions are hard to read and maintain.
Is there any simpler way to manage the test suites?
I tried to set the ignore
attribute when declaring the module, but apparently it can only be set for each test
function. I think I could disable compilation of the excluded tests by using cfg
on the module, but as the tests should always compile, I would like to avoid that.