Tests, ignore with reason

Is it possible to "report back" to the test framework that a test has been ignored for a reason?

We have a client library that has a few tests that require a (test) server. If there's no server available, I want a discreet ignored (server is not running) message rather than an error dump.

I saw some brainstorming somewhere a while back that had some kind of predicate function used to determine if a test should run. IIRC it looked something like this:

#[test]
#[skip(predicate = some_function)]
fn dostuff() {
}

This looks super sweet -- but did it ever go beyond brainstorming?

The ignore attribute takes a reason argument

https://doc.rust-lang.org/reference/attributes/testing.html#the-ignore-attribute

That would allow you to run the test when you know the service is running. But I guess what you want is a dynamic check.

Yeah, the dynamic part is important, unfortunately.

We tried simply failing the test when no (test) server is available, but it causes too much noise in the output.

tracing logs aren't captured by libtest per default. So a quick and dirty workaround (that you could abstract into a self-made #[skip] attribute macro) would be to emit a warning when the test is skipped:

#[test]
fn test_requiring_server() {
    tracing_subscriber::fmt()
        .event_format(tracing_subscriber::fmt::format().without_time())
        .with_target(false)
        .init();

    #[tracing::instrument]
    fn test_requiring_server() {
        tracing::warn!("skipped function because predicate was true");
    }

    fn predicate() -> bool {
        true
    }

    if predicate() {
        test_requiring_server();
        return;
    }

    // here the test code
    assert!(true);
}

Output:

 WARN test_requiring_server: skipped function because predicate was true
test test_requiring_server ... ok
4 Likes

That's a pretty neat trick!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.