0
点赞
收藏
分享

微信扫一扫

【每周一库】 laboratory - A simple, expressive unit test framework

本期的每周一库带来的是rust下的轻量级单元测试框架laboratory

laboratory相关链接

  • laboratory github
  • laboratory carte.io

根据laboratory在github仓库页面的介绍,它包含了单元测试框架中常用的大部分工具,例如:​​before​​​, ​​before_each​​​, ​​after​​​, ​​after_each​​等。

并且还支持不同的报告方式,其中有​​spec​​​,​​minimal​​​,​​json​​​以及​​json-pretty​​。

接下来我们试用一下laboratory库

测试环境

  • Windows 10
  • ​cargo --version​​: cargo 1.46.0-nightly (089cbb80b 2020-06-15)
  • ​rustc --version​​: rustc 1.46.0-nightly (6bb3dbfc6 2020-06-22)

我们创建一个新的rust工程,在​​main.rs​​中写入两个简单的函数

// main.rs
fn main() {
add_number(1, 2);
biger_than_10(20);
}

fn add_number (x: u64, y: u64) -> u64 { x + y }
fn biger_than_10(x: u64) -> bool { x > 10 }

#[cfg(test)]
mod tests {

use super::*;
use laboratory::{describe, it, expect};

#[test]
fn suite() {
describe("add_number()").specs(vec![
it("should return 3 when passed 1, 2", |_| {
expect(add_number(1, 2)).to_equal(3)
}),

it("should return 20 when passed 0, 20", |_| {
expect(add_number(0, 20)).to_equal(20)
})

]).in_nanoseconds().run();

describe("biger_than_10()").specs(vec![
it("should return false when passed 10", |_| {
expect(biger_than_10(10)).to_equal(false)
}),

it("should return trun when passed 100", |_| {
expect(biger_than_10(100)).to_equal(true)
})
]).in_nanoseconds().run();
}
}

这里需要注意的是,从rust程序的角度我们只定义了一个test,但是我们可以在这个test中使用laboratory库实现多个step的测试,运行结果如下图

【每周一库】 laboratory - A simple, expressive unit test framework_单元测试

以上就是本期每周一库的内容,laboratory帮助我们养成随手写测试的好习惯:)


举报

相关推荐

0 条评论