zoukankan      html  css  js  c++  java
  • Rust <5>:测试

      测试运行顺序:单元测试(同处于源文件中,以 #[cfg(tests)] 标记 mod,以 #[test] 标记 function)、集成测试(位于项目根路径下的 tests 目录下,不需要 #[cfg(tests)] 标记,但依然需要 #[test] 标记 function)、文档测试。

    一、选项

    cargo test [testN] -- --test-threads=1 --nocapture --ignored

    [testN]:可以指定单个测试模块或测试用例的完整名称单独运行,不能批量指定;但可以指定部分名称,所有名称包含此字符串的模块(包含其中所有测试用例)、测试用例均会被执行;

    --test-threads=1:默认是多线程并发运行测试用例,运行速度快,但会造成输出内容交叉,指定单线程运行可保证有序输出;

    --nocapture:默认测试通过的用例,其输出内容将会被捕获(屏蔽),指定此选项将会一同显示这些内容;

    --ignored:被标记为 #[ignore] 的测试用例默认不会被执行,指定此项以运行这些测试用例(通常是预计耗时很长的测试);

    ...

    二、单元测试示例

    #[derive(Debug)]
    pub struct Rectangle {
        length: u32,
         u32,
    }
    
    impl Rectangle {
        pub fn can_hold(&self, other: &Rectangle) -> bool {
            return self.length > other.length && self.width > other.width;
        }
    
        pub fn add_two(&mut self) {
            self.length += 2;
            self.width += 2;
        }
    }
    
    #[cfg(test)]
    mod should_panic {
        #[test]
        fn notihing() {
            println!("nothing...");
        }
    
    }
    
    #[cfg(test)]
    mod tests {
        use super::*;
    
        #[test]
        fn explotion() {
            assert_eq!(2 + 2, 4);
        }
    
        #[test]
        fn hellokitty() {
            assert_ne!(1 + 1, 4);
        }
    
        #[test]
        fn larger_can_hold_smaller() {
            let larger = Rectangle { length: 8,  7 };
            let smaller = Rectangle { length: 5,  1 };
    
            assert!(larger.can_hold(&smaller));
        }
    
        #[test]
        fn smaller_cannot_hold_larger() {
            let larger = Rectangle { length: 8,  7 };
            let smaller = Rectangle { length: 5,  1 };
    
            assert!(!smaller.can_hold(&larger));
        }
    
        #[test]
        #[ignore]
        #[should_panic(expected = "t")]
        fn should_panic() {
            panic! ("test panic");
        }
    
        #[test]
        fn print_add2() {
            let mut model = Rectangle { length: 8,  1 };
            model.add_two();
    
            assert_eq!(model.length, 10);
        }
    }

    ...

  • 相关阅读:
    ubuntu下文件安装与卸载
    webkit中的JavaScriptCore部分
    ubuntu 显示文件夹中的隐藏文件
    C语言中的fscanf函数
    test
    Use SandCastle to generate help document automatically.
    XElement Getting OuterXML and InnerXML
    XUACompatible meta 用法
    Adobe Dreamweaver CS5.5 中文版 下载 注册码
    The Difference Between jQuery’s .bind(), .live(), and .delegate()
  • 原文地址:https://www.cnblogs.com/hadex/p/8323716.html
Copyright © 2011-2022 走看看