【问题标题】:Is there a way to make it easier to debug failing, non-terminating, tests?有没有办法让调试失败的、非终止的测试变得更容易?
【发布时间】:2017-02-20 00:19:03
【问题描述】:

如果#[test] 函数意外永远循环,则测试套件不会完成。如果您杀死它(例如 ctrl-c),cargo test 似乎会静默退出,因此您既不会得到堆栈跟踪(如果启用),也不会得到哪些测试的报告通过或失败。

有没有办法让调试失败的非终止测试变得更容易?

【问题讨论】:

    标签: unit-testing rust rust-cargo


    【解决方案1】:

    我遇到了timebomb,它看起来很接近我的需要,但确实意味着手动包装每个测试;即代替:

    #[test]
    fn test() {
        assert!(true);
    }
    

    我需要做的:

    extern crate timebomb;
    use timebomb::timeout_ms;
    
    #[test]
    fn test() {
        timeout_ms(|| {
            assert!(true);
        }, 1000);
    }
    

    对于数十次测试来说,这是一种痛苦(但不可否认的是一次性)。 可是等等; Rust 有宏!这实际上似乎是一个合理的解决方案:

    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 {}
    });
    

    【讨论】:

    • 保持常规的fn 语法,使其更易于使用。还允许同时将多个测试函数括起来。
    • 很棒的发现。我一定会用这个!
    猜你喜欢
    • 2022-11-21
    • 2020-02-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 2013-01-13
    • 2017-01-10
    • 2014-12-23
    • 2011-04-05
    相关资源
    最近更新 更多