@@ -9,52 +9,81 @@ This library provides:
9
9
10
10
## Assertions and matchers
11
11
12
- Most assertions are made through the macro [ ` verify_that! ` ] . It takes two
13
- arguments: an actual value to be tested and a [ ` Matcher ` ] .
12
+ The core of GoogleTest is its * matchers* . Matchers indicate what aspect of an
13
+ actual value one is asserting: (in-)equality, containment, regular expression
14
+ matching, and so on.
15
+
16
+ To make an assertion using a matcher, GoogleTest offers three macros:
17
+
18
+ * [ ` assert_that! ` ] panics if the assertion fails, aborting the test.
19
+ * [ ` expect_that! ` ] logs an assertion failure, marking the test as having
20
+ failed, but allows the test to continue running (called a _ non-fatal
21
+ assertion_ ). It requires the use of the [ ` googletest::test ` ] [ crate::test ]
22
+ attribute macro on the test itself.
23
+ * [ ` verify_that! ` ] has no side effects and evaluates to a [ ` Result ` ] whose
24
+ ` Err ` variant describes the assertion failure, if there is one. In
25
+ combination with the
26
+ [ ` ? ` operator] ( https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator ) ,
27
+ this can be used to abort the test on assertion failure without panicking. It
28
+ is also the building block for the other two macros above.
29
+
30
+ For example:
14
31
15
- Unlike the macros used in other test assertion libraries in Rust,
16
- ` verify_that! ` does not panic when the test assertion fails. Instead, it
17
- evaluates to [ ` googletest::Result<()> ` ] [ Result ] , which the caller can choose
18
- to handle by:
32
+ ```
33
+ use googletest::prelude::*;
19
34
20
- * Returning immediately from the function with the ` ? ` operator (a * fatal*
21
- assertion), or
22
- * Logging the failure, marking the test as failed, and allowing execution to
23
- continue (see [ Non-fatal assertions] ( #non-fatal-assertions ) below).
35
+ # /* The attribute macro would prevent the function from being compiled in a doctest.
36
+ #[test]
37
+ # */
38
+ fn fails_and_panics() {
39
+ let value = 2;
40
+ assert_that!(value, eq(4));
41
+ }
24
42
25
- For example, for fatal assertions:
43
+ # /* The attribute macro would prevent the function from being compiled in a doctest.
44
+ #[googletest::test]
45
+ # */
46
+ fn two_logged_failures() {
47
+ let value = 2;
48
+ expect_that!(value, eq(4)); // Test now failed, but continues executing.
49
+ expect_that!(value, eq(5)); // Second failure is also logged.
50
+ }
26
51
27
- ```
28
- use googletest::prelude::*;
52
+ # /* The attribute macro would prevent the function from being compiled in a doctest.
53
+ #[test]
54
+ # */
55
+ fn fails_immediately_without_panic() -> Result<()> {
56
+ let value = 2;
57
+ verify_that!(value, eq(4))?; // Test fails and aborts.
58
+ verify_that!(value, eq(2))?; // Never executes.
59
+ Ok(())
60
+ }
29
61
30
62
# /* The attribute macro would prevent the function from being compiled in a doctest.
31
63
#[test]
32
64
# */
33
- fn more_than_one_failure () -> Result<()> {
65
+ fn simple_assertion () -> Result<()> {
34
66
let value = 2;
35
- verify_that!(value, eq(4))?; // Fails and ends execution of the test.
36
- verify_that!(value, eq(2)) // One can also just return the assertion result.
67
+ verify_that!(value, eq(4)) // One can also just return the last assertion.
37
68
}
38
- # more_than_one_failure().unwrap_err();
39
69
```
40
70
41
- > In case one wants behaviour closer to other Rust test libraries, the macro
42
- > [ ` assert_that! ` ] has the same parameters as [ ` verify_that! ` ] but panics on
43
- > failure.
44
-
45
71
Matchers are composable:
46
72
47
73
```
48
74
use googletest::prelude::*;
49
75
50
76
# /* The attribute macro would prevent the function from being compiled in a doctest.
51
- #[test]
77
+ #[googletest:: test]
52
78
# */
53
- fn contains_at_least_one_item_at_least_3() -> Result<()> {
79
+ fn contains_at_least_one_item_at_least_3() {
80
+ # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
54
81
let value = vec![1, 2, 3];
55
- verify_that!(value, contains(ge(3)))
82
+ expect_that!(value, contains(ge(3)));
83
+ # googletest::internal::test_outcome::TestOutcome::close_current_test_outcome::<&str>(Ok(()))
84
+ # .unwrap();
56
85
}
57
- # contains_at_least_one_item_at_least_3().unwrap() ;
86
+ # contains_at_least_one_item_at_least_3();
58
87
```
59
88
60
89
They can also be logically combined:
@@ -63,13 +92,16 @@ They can also be logically combined:
63
92
use googletest::prelude::*;
64
93
65
94
# /* The attribute macro would prevent the function from being compiled in a doctest.
66
- #[test]
95
+ #[googletest:: test]
67
96
# */
68
- fn strictly_between_9_and_11() -> Result<()> {
97
+ fn strictly_between_9_and_11() {
98
+ # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
69
99
let value = 10;
70
- verify_that!(value, gt(9).and(not(ge(11))))
100
+ expect_that!(value, gt(9).and(not(ge(11))));
101
+ # googletest::internal::test_outcome::TestOutcome::close_current_test_outcome::<&str>(Ok(()))
102
+ # .unwrap();
71
103
}
72
- # strictly_between_9_and_11().unwrap() ;
104
+ # strictly_between_9_and_11();
73
105
```
74
106
75
107
## Available matchers
@@ -236,7 +268,7 @@ impl<T: PartialEq + Debug> Matcher for MyEqMatcher<T> {
236
268
}
237
269
```
238
270
239
- The new matcher can then be used in ` verify_that! ` :
271
+ The new matcher can then be used in the assertion macros :
240
272
241
273
```
242
274
# use googletest::prelude::*;
@@ -274,12 +306,15 @@ impl<T: PartialEq + Debug> Matcher for MyEqMatcher<T> {
274
306
# MyEqMatcher { expected }
275
307
# }
276
308
# /* The attribute macro would prevent the function from being compiled in a doctest.
277
- #[test]
309
+ #[googletest:: test]
278
310
# */
279
- fn should_be_equal_by_my_definition() -> Result<()> {
280
- verify_that!(10, eq_my_way(10))
311
+ fn should_be_equal_by_my_definition() {
312
+ # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
313
+ expect_that!(10, eq_my_way(10));
314
+ # googletest::internal::test_outcome::TestOutcome::close_current_test_outcome::<&str>(Ok(()))
315
+ # .unwrap();
281
316
}
282
- # should_be_equal_by_my_definition().unwrap() ;
317
+ # should_be_equal_by_my_definition();
283
318
```
284
319
285
320
## Non-fatal assertions
@@ -290,8 +325,8 @@ having failed, but execution continues until the test completes or otherwise
290
325
aborts.
291
326
292
327
To make a non-fatal assertion, use the macro [ ` expect_that! ` ] . The test must
293
- also be marked with [ ` googletest::test ` ] [ test ] instead of the Rust-standard
294
- ` #[test] ` .
328
+ also be marked with [ ` googletest::test ` ] [ crate:: test] instead of the
329
+ Rust-standard ` #[test] ` .
295
330
296
331
``` no_run
297
332
use googletest::prelude::*;
0 commit comments