Is there a recommended way to tag some tests and run them by specifying tag(s)

Is there a recommended way to tag tests and run them by specifying the tag(s)?

For example:

#[test(tag = "fast")]
fn test_xxx() {
    ...
}

And then run the tests with a command like: cargo test --tag fast?

I found multiple approaches to achieve this goal, but none of them seem particularly idiomatic to me. Is there an idiomatic or recommended way to do this in Rust? Does any up-to-date and active test framework in Rust already support this functionality? Or is there any plan for Rust to add features like this to its standard library and toolchain?

#[test] fn fast_something() {} and cargo test fast will run it.

The argument to cargo test is a substring for filtering tests by name.

Alternatively, you can use #[cfg_attr(not(feature = "fast-tests"), ignore)], add the feature to Cargo.toml and run cargo test -F fast-tests.

3 Likes

Thanks. Today I did some research and found these two ways. They are workable, but they are the ways I think not idiomatic. A tag is like some extra info or group name, and add it to every test function name is not the semantics I want. I personally also don't like the feature way. I prefer some code looks more straightforward. If no standard and simple way, I'd like to use a build script, and pass the cargo --config test_tag=fast through environment variable to a procedural macro and make the code like:

#[test_tag = "fast"]
#[test]
fn test_xxx() {
    ...
}

or

#[my_test(tag = "fast")]
fn test_xxx() {
    ...
}

Use command cargo test --config test_tag=fast.

and this test will be removed if the passed in tag is not β€œfast” when expanding the macro.

There are no tags in Cargo tests.

Cargo is stuck having a primitive internal interface for tests, and due to that interface and backward compatibility concerns, any test improvements are taking a lot of time, and get blocked on many design and implementation issues. It has been like that for 7 years now:

Cargo as a project is understaffed and is struggling to even triage outside contributions, so don't hold your breath for new test features.

2 Likes

Thanks for your information! It is very helpful.