If a #[test] function accidentally loops forever, the test suite doesn't finish. If you kill it (eg ctrl-c), cargo test seems to exit silently, so you get neither a stack trace (if enabled) or a report of which tests passed or failed.
Is there a way to make it easier to debug failing, non-terminating, tests?
I've come across timebomb which looks like close to what I need, but does mean manually wrapping every test; i.e. instead of:
#[test]
fn test() {
assert!(true);
}
I need to do:
extern crate timebomb;
use timebomb::timeout_ms;
#[test]
fn test() {
timeout_ms(|| {
assert!(true);
}, 1000);
}
which is a pain (but admittedly one-time) for dozens of tests. But wait; Rust has macros! This actually seems like a reasonable solution:
extern crate timebomb;
use timebomb::timeout_ms;
macro_rules! timeout_test {
( $name:ident() $code:block ) => {
#[test]
fn $name() {
timeout_ms(|| $code, 1000);
}
}
}
// the hard way
#[test]
fn foo() {
timeout_ms(|| {
loop {}
}, 1000);
}
// the now easy way
timeout_test!(bar() {
loop {}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With