#单元测试 #测试框架 #单元 #规范 #调试 #测试

开发 实验室

一个简单、易于表达的 Rust 单元测试框架

6 个版本 (稳定)

2.0.0 2021 年 4 月 26 日
1.3.0 2020 年 9 月 19 日
0.1.0 2020 年 9 月 14 日

排名 357 位,在 测试

Download history 50/week @ 2024-03-11 30/week @ 2024-03-18 28/week @ 2024-03-25 49/week @ 2024-04-01 28/week @ 2024-04-08 10/week @ 2024-04-15 16/week @ 2024-04-22 11/week @ 2024-04-29 18/week @ 2024-05-06 58/week @ 2024-05-13 44/week @ 2024-05-20 25/week @ 2024-05-27 37/week @ 2024-06-03 28/week @ 2024-06-10 30/week @ 2024-06-17 49/week @ 2024-06-24

每月下载 144
7 crate 使用

MIT 许可证

48KB
1.5K SLoC

laboratory

一个简单、易于表达的 Rust 单元测试框架

GitHub Workflow Status (branch) Crates.io Crates.io

请查看 文档 和 github 上的详细 示例

实验室是一个层 2 测试执行器解决方案,它位于 Rust 测试执行器之上,提供无与伦比的功能和易于使用的体验。

功能

  • before_all, before_each, after_all, after_each 钩子
  • 不同的报告选项:spec, minimal, json, json-pretty, rust, dot, tap, list
  • 在纳秒、微秒、毫秒和秒中报告测试时长
  • 使用自定义断言库
  • 排除测试
  • 嵌套测试套件
  • 测试重试支持
  • 使用状态
  • "should panic" 测试
  • 控制台高亮显示
  • 动态测试
  • 突出显示慢速测试
  • 没有奇怪的宏需要尝试理解或调试!
  • 人类可读的代码和测试结果

安装

在 Cargo.toml 中

[dev-dependencies]
laboratory = "2.0.0"

然后在您的测试文件中

#[cfg(test)]
mod tests {
    use laboratory::{describe, describe_skip, it, it_skip, it_only, expect};
}

入门

测试一个简单的函数

// from examples/simple.rs
fn main() {
    add_one(0);
}

// Here we have one function that does
// one thing: Adds one to whatever number
// we pass to it.
fn add_one (n: u64) -> u64 { n + 1 }

#[cfg(test)]
mod tests {

    // lets pull our add_one function into scope
    use super::*;

    // now let's pull in our lab tools into scope
    // to test our function
    use laboratory::{describe, expect, LabResult, NullState};

    // From Rust's perspective we will only define
    // one test, but inside this test we can define
    // however many tests we need.
    #[test]
    fn suite() -> LabResult {

        // let's describe what our add_one() function will do.
        // The describe function takes a closure as its second
        // argument. And that closure also takes an argument which
        // we will call "suite". The argument is the suite's context
        // and it allows for extensive customizations. The context struct
        // comes with a method called it() and using this method we can
        // define a test.
        describe("add_one()", |suite| {

            // when describing what it should do, feel free to be
            // as expressive as you would like.
            suite.it("should return 1 when passed 0", |_| {

                // here we will use the default expect function
                // that comes with laboratory.
                // We expect the result of add_one(0) to equal 1
                expect(add_one(0)).to_equal(1)

            })

            // just as a sanity check, let's add a second test
            .it("should return 2 when passed 1", |_| {

                expect(add_one(1)).to_equal(2)

            });

        }).state(NullState).milis().run()

    }
}

然后运行

$ cargo test -- --nocapture

结果


running 1 test


### Lab Results Start ###

add_one()
  ✓  should return 1 when passed 0 (0ms)
  ✓  should return 2 when passed 1 (0ms)2 tests completed (0ms)

### Lab Results End ###


test tests::suite ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


依赖项

~2.2–3.5MB
~59K SLoC