GoogleTest
جعبه GoogleTest با استفاده از matchers اجازه میدهد تا assertهای آزمایشی انعطافپذیر را انجام دهید:
use googletest::prelude::*;
#[googletest::test]
fn test_elements_are() {
let value = vec!["foo", "bar", "baz"];
expect_that!(value, elements_are!(eq(&"foo"), lt(&"xyz"), starts_with("a")));
}
اگر آخرین عنصر را به"!"تغییر دهیم، آزمایش با یک پیغام خطای ساختار یافته که خطا را pin-pointing میکند، شکست می خورد:
---- test_elements_are stdout ----
Value of: value
Expected: has elements:
0. is equal to "foo"
1. is less than "xyz"
2. starts with prefix "!"
Actual: ["foo", "bar", "baz"],
where element #2 is "baz", which does not start with "!"
at src/testing/googletest.rs:6:5
Error: See failure output above
-
GoogleTest بخشی از Rust Playground نیست، بنابراین باید این مثال را در یک محیط local اجرا کنید. برای افزودن سریع آن به پروژه Cargo موجود، از
cargo add googletestاستفاده کنید. -
خط
use googletest::prelude::*;تعدادی از ماکروها و typeهای پرکاربرد را وارد میکند. -
This just scratches the surface, there are many builtin matchers. Consider going through the first chapter of “Advanced testing for Rust applications”, a self-guided Rust course: it provides a guided introduction to the library, with exercises to help you get comfortable with
googletestmacros, its matchers and its overall philosophy. -
یک ویژگی خاص خوب این است که عدم تطابق در stringهای چند خطی به صورت یک تفاوت نشان داده می شود:
#[test]
fn test_multiline_string_diff() {
let haiku = "Memory safety found,\n\
Rust's strong typing guides the way,\n\
Secure code you'll write.";
assert_that!(
haiku,
eq("Memory safety found,\n\
Rust's silly humor guides the way,\n\
Secure code you'll write.")
);
}
تفاوت رنگی را نشان میدهد (رنگ ها در اینجا نشانداده نمیشوند):
Value of: haiku
Expected: is equal to "Memory safety found,\nRust's silly humor guides the way,\nSecure code you'll write."
Actual: "Memory safety found,\nRust's strong typing guides the way,\nSecure code you'll write.",
which isn't equal to "Memory safety found,\nRust's silly humor guides the way,\nSecure code you'll write."
Difference(-actual / +expected):
Memory safety found,
-Rust's strong typing guides the way,
+Rust's silly humor guides the way,
Secure code you'll write.
at src/testing/googletest.rs:17:5
- این crate یک پورت [GoogleTest for C++](https://google.github.io/googletest/) در Rust است.