Limit execution time of test?

Is there a way to set a maximum time for a test? Something like this https://github.com/junit-team/junit4/wiki/timeout-for-tests

Thanks!

1 Like

That was the subject of this earlier thread.

@hoodie Did you ever get the time limiting working for you?

I didn't pursue this much further.
What I meant to do was start my test in a thread and kill it after a certain time.
In that thread we pointed out how to kill a thread, though I never actually tried it at home :smiley:
It would be cool if this was a feature of Rusts testing functionality though.

See Test timeouts · Issue #2798 · rust-lang/rfcs · GitHub

Based on the code from the linked issue, one can easily create a macro decorator, giving:

custom_test! {
    #[test(timeout = Duration::from_millis(50))]
    fn panic ()
    {
        panic!();
    }
}
custom_test! {
    #[test(timeout = Duration::from_millis(50))]
    fn fine ()
    {}
}
custom_test! {
    #[test]
    fn slow_no_timeout ()
    {
        ::std::thread::sleep(::std::time::Duration::from_millis(500));
    }
}

and with macro_rules_attribute - Rust, we can even write it as:

#[macro_use]
extern crate macro_rules_attribute;

#[apply(custom_test)]
#[test(timeout = Duration::from_millis(50))]
fn infinite_loop ()
{
    loop {}
}

Playground

3 Likes

That's ace.

I came across this thread while looking for a solution for test timeouts. A colleague of mine later found this attribute in the ntest crate which seems apt.