@@ -23,16 +23,16 @@ subclass _or_ `@Test`, and it will dynamically detect what context it is running
23
23
the correct test failure:
24
24
25
25
``` swift
26
+ @Test
27
+ func testFeature () {
28
+ assertSnapshot (of : MyView (), as : .image ) // ✅
29
+ }
30
+
26
31
class FeatureTests : XCTestCase {
27
32
func testFeature () {
28
33
assertSnapshot (of : MyView (), as : .image ) // ✅
29
34
}
30
35
}
31
-
32
- @Test
33
- func testFeature () {
34
- assertSnapshot (of : MyView (), as : .image ) // ✅
35
- }
36
36
```
37
37
38
38
### Configuring snapshots
@@ -48,17 +48,27 @@ message that allows you to quickly open a diff of two files, such as
48
48
assertion so that new snapshots are generated and saved to disk.
49
49
50
50
These properties can be overridden for a scope of an operation using the
51
- `` withSnapshotTesting(record:diffTool:operation:)-2kuyr `` function. In an XCTest context the
52
- simplest way to do this is to override the ` invokeTest ` method on ` XCTestCase ` and wrap it in
53
- ` withSnapshotTesting ` :
51
+ `` withSnapshotTesting(record:diffTool:operation:)-2kuyr `` function. In a Swift Testing context
52
+ you can apply the `` Testing/Trait/snapshots `` trait to either a single test or an entire suite:
53
+
54
+ ``` swift
55
+ import SnapshotTesting
56
+
57
+ @Suite (.snapshots (record : .failed , diffTool : .ksdiff ))
58
+ struct FeatureTests {
59
+ …
60
+ }
61
+ ```
62
+
63
+ This will override the ` diffTool ` and ` record ` properties for each test in the suite.
64
+
65
+ In an XCTest context, the simplest way to do this is to override the ` invokeTest ` method on
66
+ ` XCTestCase ` and wrap it in ` withSnapshotTesting ` :
54
67
55
68
``` swift
56
69
class FeatureTests : XCTestCase {
57
70
override func invokeTest () {
58
- withSnapshotTesting (
59
- record : .missing ,
60
- diffTool : .ksdiff
61
- ) {
71
+ withSnapshotTesting (record : .failed , diffTool : .ksdiff ) {
62
72
super .invokeTest ()
63
73
}
64
74
}
@@ -67,14 +77,27 @@ class FeatureTests: XCTestCase {
67
77
68
78
This will override the ` diffTool ` and ` record ` properties for each test function.
69
79
70
- Swift's new testing framework also allows for this kind of configuration, both for a single test
71
- and an entire test suite. This is done via what are known as "test traits":
80
+ ### UI Testing
72
81
73
- ``` swift
74
- import SnapshotTesting
82
+ Xcode's UI testing tools are currently incompatible with Swift Testing. Simply adding
83
+ ` import Testing ` to any UI test target file will cause a compilation error saying that "Testing"
84
+ cannot be found. This complicates using SnapshotTesting in UI test targets because it needs to
85
+ import Testing in order to provide the test helpers mentioned above.
75
86
76
- @Suite (.snapshots (record : .all , diffTool : .ksdiff ))
77
- struct FeatureTests {
78
- …
79
- }
87
+ The way in which Xcode disallows importing Testing in UI test targets is via the presence of a
88
+ special Swift flag:
89
+
90
+ ```
91
+ -module_alias Testing=_Testing_Unavailable
80
92
```
93
+
94
+ This is done so that people do not expect ` #expect ` and other Testing tools to work in UI test
95
+ targets. If you want to use SnapshotTesting in a UI test target, we recommend that you remove
96
+ this flag:
97
+
98
+ * Open your project's settings and navigate to the settings for your UI testing target.
99
+ * Search for "Other Swift flags" in the "Build Settings" tab.
100
+ * Delete the ` $(inherited) ` flag.
101
+
102
+ Now you can ` import SnapshotTesting ` in UI test targets _ and_ make use of ` assertSnapshot ` . But
103
+ do remember that you _ cannot_ use ` #expect ` or any of the other tools from Swift Testing.
0 commit comments