zoukankan      html  css  js  c++  java
  • 【Rust】单元测试

    环境

    • Rust 1.56.1
    • VSCode 1.61.2

    概念

    参考:https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html

    示例

    测试相等

    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
    
    #[cfg(test)]
    mod tests {
    
        use super::*;
    
        #[test]
        fn test_add() {
            assert_eq!(add(1, 2), 3);
        }
    }
    
    fn main() {}
    

    测试恐慌

    pub fn divide_non_zero_result(a: u32, b: u32) -> u32 {
        if b == 0 {
            panic!("Divide-by-zero error");
        } else if a < b {
            panic!("Divide result is zero");
        }
        a / b
    }
    
    #[cfg(test)]
    mod tests {
        use super::*;
    
        #[test]
        #[should_panic]
        fn test_any_panic() {
            divide_non_zero_result(1, 0);
        }
    
        #[test]
        #[should_panic(expected = "Divide result is zero")]
        fn test_specific_panic() {
            divide_non_zero_result(1, 10);
        }
    }
    
    fn main() {}
    

    运行一个测试

    C:\Users\jiangbo\work\rust>cargo test test_any_panic
        Finished test [unoptimized + debuginfo] target(s) in 0.01s
         Running unittests (target\debug\deps\rust-ee9e808a3f21b94a.exe)
    
    running 1 test
    test tests::test_any_panic - should panic ... ok
    
    test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.00s
    

    运行多个测试

    C:\Users\jiangbo\work\rust>cargo test panic
        Finished test [unoptimized + debuginfo] target(s) in 0.01s
         Running unittests (target\debug\deps\rust-ee9e808a3f21b94a.exe)
    
    running 2 tests
    test tests::test_any_panic - should panic ... ok
    test tests::test_specific_panic - should panic ... ok
    
    test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
    
    

    忽略测试

    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
    
    #[cfg(test)]
    mod tests {
        use super::*;
        #[test]
        #[ignore]
        fn ignored_test() {
            assert_eq!(add(0, 0), 0);
        }
    }
    
    fn main() {}
    

    总结

    了解了 Rust 中外部函数接口的使用。

    附录

  • 相关阅读:
    windows基线检测脚本编写指南-powershell版
    2020蓝帽杯CTF Web 题
    2020天翼杯CTF Web 题
    Firefox 密码脱取
    利用iptables做端口转发
    Flask 快速学习笔记
    JSP无%%号命令执行一句话
    linux 后渗透凭据收集
    Fastjson漏洞利用
    Gogs远程命令执行利用
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/15744134.html
Copyright © 2011-2022 走看看