@@ -10,15 +10,49 @@ See https://swift.org/LICENSE.txt for license information
10
10
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
11
11
-->
12
12
13
- Check for expected values and outcomes in tests.
13
+ Check for expected values, outcomes, and asynchronous events in tests.
14
14
15
15
## Overview
16
16
17
- The testing library provides ` #expect() ` and ` #require() ` macros you use to
18
- validate expected outcomes. To validate that an error is thrown, or _ not_ thrown,
19
- the testing library provides several overloads of the macros that you can use.
20
- Use a `` Confirmation `` to confirm the occurrence of an asynchronous event that
21
- you can't check directly using an expectation.
17
+ Use `` expect(_:_:sourceLocation:) `` and
18
+ `` require(_:_:sourceLocation:)-5l63q `` macros to validate expected
19
+ outcomes. To validate that an error is thrown, or _ not_ thrown, the
20
+ testing library provides several overloads of the macros that you can
21
+ use. For more information, see < doc:testing-for-errors-in-swift-code > .
22
+
23
+ Use a `` Confirmation `` to confirm the occurrence of an
24
+ asynchronous event that you can't check directly using an expectation.
25
+ For more information, see < doc:testing-asynchronous-code > .
26
+
27
+ ### Validate your code's result
28
+
29
+ To validate that your code produces an expected value, use
30
+ `` expect(_:_:sourceLocation:) `` . `` expect(_:_:sourceLocation:) `` captures the
31
+ expression you pass, and provides detailed information when the code doesn't
32
+ satisfy the expectation.
33
+
34
+ ``` swift
35
+ @Test func calculatingOrderTotal () {
36
+ let calculator = OrderCalculator ()
37
+ #expect (calculator.total (of : [3 , 3 ]) == 7 )
38
+ // Prints "Expectation failed: (calculator.total(of: [3, 3]) → 6) == 7"
39
+ }
40
+ ```
41
+
42
+ Your test keeps running after `` expect(_:_:sourceLocation:) `` fails. To stop
43
+ the test when the code doesn't satisfy a requirement, use
44
+ `` require(_:_:sourceLocation:)-5l63q `` instead:
45
+
46
+ ``` swift
47
+ @Test func returningCustomerRemembersUsualOrder () throws {
48
+ let customer = try #require (Customer (id : 123 ))
49
+ // The test runner doesn't reach this line if the customer is nil.
50
+ #expect (customer.usualOrder .countOfItems == 2 )
51
+ }
52
+ ```
53
+
54
+ `` require(_:_:sourceLocation:)-5l63q `` throws an instance of
55
+ `` ExpectationFailedError `` when your code fails to satisfy the requirement.
22
56
23
57
## Topics
24
58
@@ -30,6 +64,7 @@ you can't check directly using an expectation.
30
64
31
65
### Checking that errors are thrown
32
66
67
+ - < doc:testing-for-errors-in-swift-code >
33
68
- `` expect(throws:_:sourceLocation:performing:)-79piu ``
34
69
- `` expect(throws:_:sourceLocation:performing:)-1xr34 ``
35
70
- `` expect(_:sourceLocation:performing:throws:) ``
@@ -41,6 +76,7 @@ you can't check directly using an expectation.
41
76
42
77
### Confirming that asynchronous events occur
43
78
79
+ - ``< doc:testing-asynchronous-code >
44
80
- `` confirmation(_:expectedCount:fileID:filePath:line:column:_:) ``
45
81
- `` Confirmation ``
46
82
0 commit comments