GoogleTest
The GoogleTest crate allows for flexible test assertions using matchers:
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("b")));
}
๋ง์ง๋ง ์์๋ฅผ '!'
๋ก ๋ณ๊ฒฝํ๋ฉด ํ
์คํธ๊ฐ ์คํจํ๊ณ ์ค๋ฅ๋ฅผ ์ ํํ ๊ฐ๋ฆฌํค๋ ๊ตฌ์กฐํ๋ ์ค๋ฅ ๋ฉ์์ง๊ฐ ํ์๋ฉ๋๋ค.
---- 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 ํ๋ ์ด๊ทธ๋ผ์ด๋์ ์ผ๋ถ๊ฐ ์๋๋ฏ๋ก ๋ก์ปฌ ํ๊ฒฝ์์ ์ด ์๋ฅผ ์คํํด์ผ ํฉ๋๋ค.
cargo add googletest
๋ฅผ ์ฌ์ฉํ์ฌ ๊ธฐ์กด Cargo ํ๋ก์ ํธ์ ๋น ๋ฅด๊ฒ ์ถ๊ฐํ์ธ์. -
use googletest::prelude::*;
์ค์ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ๋งคํฌ๋ก ๋ฐ ํ์ ์ ์ฌ๋ฌ ๊ฐ ๊ฐ์ ธ์ต๋๋ค. -
์ด๋ ์ผ๋ถ์ผ ๋ฟ์ด๋ฉฐ ๋ด์ฅ๋ ๋งค์ฒ๊ฐ ๋ง์ด ์์ต๋๋ค.
-
A particularly nice feature is that mismatches in multi-line strings are shown as a diff:
#[test]
fn test_multiline_string_diff() {
let haiku = "Memory safete 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.")
);
}
์์์ผ๋ก ๊ตฌ๋ถ๋ diff๋ฅผ ํ์ํฉ๋๋ค(์ฌ๊ธฐ์์๋ ์์์ด ํ์๋์ง ์์ต๋๋ค).
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
- ํฌ๋ ์ดํธ๋ C++์ฉ GoogleTest์ Rust ํฌํธ์ ๋๋ค.