#testing #testing-utilities #file #assertions #setup #projects

common-testing

跨项目复用的常见测试快捷方式和工具

5 个版本 (3 个稳定版)

1.1.1 2023 年 12 月 30 日
1.0.0 2023 年 12 月 30 日
0.2.0 2023 年 12 月 25 日
0.1.0 2023 年 12 月 24 日

#303 in 文件系统


2 crates 使用

Apache-2.0

33KB
431 代码行

常见测试

跨项目复用的常见测试快捷方式和工具。免费分享此代码或将它复制到您的项目中。

如果您在多个项目中发现了有用的函数,考虑将其添加到此仓库中。

特性亮点

Result 和 Option 断言

断言 Result 或 Option 处于特定状态。在断言处于预期状态后,还有将 Result 或 Option 转换为内部值的 into 函数。

  • assert::ok
  • assert::ok_into
  • assert::err
  • assert::err_into
  • assert::some
  • assert::some_into
  • assert::none

use common_testing::assert;

#[test]
fn test_1() {
  let result: Result<u32, String> = Ok(1);
  assert::ok(&result);

  let result: Result<u32, String> = Ok(1);
  let ok = assert::ok_into(&result);
  assert::equal(ok, 1);

  let result: Result<u32, String> = Err("error".to_string());
  assert::err(&result);

  let result: Result<u32, String> = Err("error".to_string());
  let error = assert::err_into(&result);
  assert::equal(error, "error".to_string());

  let result: Option<u32> = Some(1);
  assert::some(&result);

  let result: Option<u32> = Some(1);
  let some = assert::some_into(&result);
  assert::equal(some, 1);

  let result: Option<u32> = None;
  assert::none(&result);
}

COW 断言

断言 COW 处于特定状态。

  • assert::borrowed
  • assert::owned
use common_testing::assert;

#[test]
fn test_1() {
  let cow: Cow<str> = Cow::Borrowed("borrowed");
  assert::borrowed(&cow);

  let cow: Cow<str> = Cow::Owned("owned".to_string());
  assert::owned(&cow);
}

AsRef 断言

断言两个类型之间存在公共的 AsRef 实现并且 AsRef 值相等。如果存在多个可能的 AsRef 实现,请指定类型。如果只有一个可能的 AsRef 实现,类型将被推断。

use common_testing::assert;

#[test]
fn test_1() {
  let my_string = "abc";

  // When there is more than one AsRef possible, say which one.
  assert::ref_equal::<str>(&my_string, &"abc");
  assert::ref_equal::<str>(&my_string.to_string(), &"abc".to_string());

  // When there is only one AsRef possible, the type is inferred.
  assert::ref_equal(&my_string, &b"abc");
}

测试全局变量、操作系统调用或文件系统

Rust 通常会并行运行测试,这可能会在测试未隔离时引起问题。此库提供了一种顺序运行测试的方法,这对于测试使用全局变量、操作系统调用或文件系统的代码非常有用。

如果您希望测试顺序运行,请使用 setup::sequential 函数。这将返回一个锁,将阻止其他使用 setup::sequential 的测试同时运行。

  • setup::sequential
use common_testing::setup;

#[test]
fn test_1() {
 let _lock = setup::sequential();
 // test code
}

#[test]
fn test_2() {
 let _lock = setup::sequential();
 // test code
}

严格相等断言

此库提供严格相等断言,如果类型不可与 PartialEq 进行比较,则将在编译时失败。

自动展开 Result 和 Option,如果值是 Err 或 None,则测试将失败。这对于在测试中删除样板 .unwrap().unwrap() 调用非常有用。

将错误信息标准化,始终打印值比较,并要求值为引用以防止断言意外获取所有权。

  • assert::equal
  • assert::not_equal
  • assert::default
use common_testing::assert;

#[test]
fn test_1() {
  // Has a useful comparison message, uses pretty_assertions to display
  // diffs when the test fails.
  assert::equal(&1, &1);
  assert::not_equal(&1, &2);

  // Automatically unwraps Result and Option, failing
  // the test if the value is Err or None.
  assert::equal(Result::Ok(1), 1);
  assert::equal(Option::Some(1), 1);
  assert::equal(Option::Some(Result::Ok(1)), 1);
  assert::equal(Result::Ok(Option::Some(1)), 1);

  // Assert a value is equal to the default value for i's type,
  // useful for testing implementations that use `::default()`.
  let i = 0;
  assert::default(i);
}

文件设置和断言

编写了太多带有特定模板的测试,用于处理固定值、文件或大量数据,导致期望不固定或副作用难以维护或难以调试异步代码。此库提供可重用的文件处理功能,鼓励最佳实践以减少测试之间的副作用或变异性。

  • assert::equal_file_contents - 与固定值或大量数据进行比较。

  • assert::cursor_completely_consumed - 捕获某些非常常见的错误。

  • setup::get_file_contents - 将固定值或大量数据读取到测试中。

  • setup::create_dir_all - 确保测试的目录路径存在。

  • setup::remove_file - 如果存在则删除文件,对重置测试很有用。

  • setup::write_file_contents - 为测试创建临时文件。

use common_testing::assert;

#[test]
fn test_1() {
 let result1 = "some file contents";
 assert::equal_file_contents(&result, "./fixtures/test_file1");

 // cursor_completely_consumed is useful for testing parsers.
  let mut cursor = Cursor::new("abc");
  cursor.read_u8().unwrap();
  cursor.read_u8().unwrap();
  cursor.read_u8().unwrap();
  assert::cursor_completely_consumed(&cursor);
}

二进制数据断言

用于处理二进制数据的断言,对数据类型做出假设以使失败信息更有用。

  • assert::equal_bytes
  • assert::equal_hex_bytes
use common_testing::assert;

#[test]
fn test_1() {
  // equal_bytes and equal_hex_bytes are useful for testing binary data.
  let result1 = "some file contents";
  let result2 = "some file contents";
  assert::equal_bytes(&result1, &result2);

  let result1 = "some file contents";
  let result2 = "736f6d652066696c6520636f6e74656e7473";
  assert::equal_hex_bytes(&result1, &result2);
}

贡献和架构决策

此库旨在成为一组有用的测试实用工具。如果您有一个在多个项目中都发现很有用的函数,请考虑将其添加到这个存储库中。

  • 请注意,此库旨在成为一组实用工具的集合,而不是一个框架。具体来说,如果您正在添加新概念或想法,请考虑为其创建一个新的存储库。

  • 如果您有一个在多个项目中都没有用处的实用工具,请考虑将其保留在使用它的项目中。

  • 如果您使用宏,请考虑将它们隔离在专用存储库中,而不是将其添加到本存储库中。宏更难维护和测试,可能成为新贡献者的障碍,并且往往会以难以删除或升级的方式在整个项目中传播。

请参阅变更日志

依赖关系

~0.9–1.5MB
~27K SLoC