#断言 #流畅 #测试 #测试驱动开发 #简洁 #接口 #编写

fluent-asserter

一个使用流畅接口编写测试断言的库

10 个版本

0.1.9 2022年4月22日
0.1.8 2022年4月22日
0.1.7 2022年3月23日
0.1.0 2021年12月6日

Rust 模式 中排名 1204

Download history 40/week @ 2024-03-13 75/week @ 2024-03-20 59/week @ 2024-03-27 80/week @ 2024-04-03 59/week @ 2024-04-10 47/week @ 2024-04-17 38/week @ 2024-04-24 58/week @ 2024-05-01 31/week @ 2024-05-08 89/week @ 2024-05-15 178/week @ 2024-05-22 79/week @ 2024-05-29 213/week @ 2024-06-05 322/week @ 2024-06-12 191/week @ 2024-06-19 339/week @ 2024-06-26

每月下载量 1,074
mc-build-rs 中使用

Apache-2.0

42KB
737

fluent-asserter

github crates.io docs.rs build status

简介

一个用于编写测试断言的库,具有流畅的接口。编写简洁的测试与编写简洁的生产代码一样重要。此库包含多种 Rust 类型的测试断言器,以便在自动化测试中产生简洁的断言。它还有助于提升测试驱动开发(TDD)体验,从而产生干净、舒适且易于维护的测试。

使用方法

将依赖项添加到您的 Cargo.toml

[dependencies]
fluent-asserter = "0.1.9"

然后通过预导入导入断言器

use fluent_asserter::prelude::*;

现在您应该能够在测试中使用流畅的语法编写测试断言。在下一节中,我将向您展示如何操作。

字符串和字符串切片断言

您可以编写 String 和 str slices 的字符串断言

#[test]
fn string_assertions() {
    assert_that!("Life tastes great!").is_equal_to("Life tastes great!");
    assert_that!("Life tastes great!").contains("great");
    assert_that!("Life tastes great!").starts_with("Life");
    assert_that!("Life tastes great!").ends_with("!");
    assert_that!("Life tastes great!").is_not_empty();
    assert_that!("Life tastes great!").has_length(18);
    assert_that!("Life tastes great!").contains_any(&["Life", "awesome"]);
    assert_that!("Life tastes great!").contains_all(&["Life", "tastes", "great!"]);
}

数字断言

#[test]
fn number_assertions() {
   assert_that!(21).is_equal_to(21);
   assert_that!(21).is_smaller_than(22);
   assert_that!(21).is_smaller_than_or_equal_to(21);
   assert_that!(21).is_greater_than(20);
   assert_that!(21).is_in_range(21,31);
   assert_that!(21).is_not_in_range(10,20);
   assert_that!(3.14159).is_approx_equal(3.142, 0.001);
}

布尔断言

#[test]
fn boolean_assertions() {
   assert_that!(true).is_true();
   assert_that!(false).is_false();
}

panic 断言

#[test]
fn panic_assertions() {
    assert_that_code!(|| panic!("An error occurred!"))
        .panics()
        .with_message("An error occurred!");

    assert_that_code!(|| println!("Life tastes great!")).does_not_panic();
}

迭代器断言

#[test]
fn iterator_assertions() {
    assert_that!(vec!["tasty", "delicious", "lovely"]).is_equal_to(vec!["tasty", "delicious", "lovely"]);
    assert_that!(vec!["tasty", "delicious", "lovely"]).contains("delicious");
    assert_that!(vec!["tasty", "delicious", "lovely"]).contains_all(&["tasty", "delicious", "lovely"]);
    assert_that!(vec!["tasty", "delicious", "lovely"]).has_count(3);
    assert_that!(vec!["tasty", "delicious", "lovely"]).does_not_contain_any(&["awesome", "amazing"]);
    assert_that!(vec!["tasty", "delicious", "lovely"]).is_not_empty();
}

结构体迭代器断言

#[derive(Clone)]
struct Person {
    name: String,
    age: i32,
}

#[test]
fn iterator_assertion_for_struct() {
    let people: Vec<Person> = vec![
        Person {
            name: String::from("Daniel"),
            age: 32,
        },
        Person {
            name: String::from("Jimmy"),
            age: 45,
        },
    ];

    assert_that!(people).satisfies_respectively(with_asserters!(
            |person1: &Person| {
                assert_that!(&person1.name).is_equal_to(&String::from("Daniel"));
                assert_that!(&person1.age).is_equal_to(&32);
            },
            |person2: &Person| {
                assert_that!(&person2.name).is_equal_to(&String::from("Jimmy"));
                assert_that!(&person2.age).is_equal_to(&45);
            }
        ));
}

哈希表断言

#[test]
fn hashmap_assertions() {
    let mut hash_map = HashMap::<String, String>::new();
    assert_that!(&hash_map).is_empty();

    hash_map.insert(String::from("key"), String::from("value"));
    assert_that!(&hash_map).has_length(1);
    assert_that!(&hash_map).is_not_empty();
    assert_that!(&hash_map).contains_key(&String::from("key"));
    assert_that!(&hash_map).does_not_contain_key(String::from("key2"));
}

Option 断言

#[test]
fn option_assertions() {
    let option = Option::Some("Winner!");
    assert_that!(option).is_some();
    assert_that!(option).is_some_with_value("Winner!");

    let none = Option::<i32>::None;
    assert_that!(none).is_none();
}

Result 断言

#[test]
pub fn result_assertions() {
    let result : Result<i32,i32> = Ok(3);
    assert_that!(&result).is_ok();
    assert_that!(&result).is_ok_with_value(3);

    let error : Result<i32,String> = Err(String::from("error message"));
    assert_that!(&error).is_error();
    assert_that!(&error).is_error_with_value(String::from("error message"));
}

清晰简洁的错误信息

在断言失败的情况下,错误信息清晰且直接,包含与领域主题相关的所有信息。

#[test]
fn test() {
   let string_variable = String::from("Hello Rust!");

   assert_that!(string_variable).is_equal_to(String::from("Hello C#!"));
}

此测试产生了以下断言错误信息

Expected string_variable to be "Hello C#!", but was "Hello Rust!".

依赖关系

~475KB