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
This slide should take about 5 minutes.
  • 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 ํฌํŠธ์ž…๋‹ˆ๋‹ค.