Skip to content

Commit 6a77fc2

Browse files
Add docs for UI testing. (#989)
* Add docs for UI testing. * Update IntegratingWithTestFrameworks.md --------- Co-authored-by: Stephen Celis <[email protected]>
1 parent 1be8144 commit 6a77fc2

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

Sources/SnapshotTesting/Documentation.docc/Articles/IntegratingWithTestFrameworks.md

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ subclass _or_ `@Test`, and it will dynamically detect what context it is running
2323
the correct test failure:
2424

2525
```swift
26+
@Test
27+
func testFeature() {
28+
assertSnapshot(of: MyView(), as: .image) //
29+
}
30+
2631
class FeatureTests: XCTestCase {
2732
func testFeature() {
2833
assertSnapshot(of: MyView(), as: .image) //
2934
}
3035
}
31-
32-
@Test
33-
func testFeature() {
34-
assertSnapshot(of: MyView(), as: .image) //
35-
}
3636
```
3737

3838
### Configuring snapshots
@@ -48,17 +48,27 @@ message that allows you to quickly open a diff of two files, such as
4848
assertion so that new snapshots are generated and saved to disk.
4949

5050
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`:
5467

5568
```swift
5669
class FeatureTests: XCTestCase {
5770
override func invokeTest() {
58-
withSnapshotTesting(
59-
record: .missing,
60-
diffTool: .ksdiff
61-
) {
71+
withSnapshotTesting(record: .failed, diffTool: .ksdiff) {
6272
super.invokeTest()
6373
}
6474
}
@@ -67,14 +77,27 @@ class FeatureTests: XCTestCase {
6777

6878
This will override the `diffTool` and `record` properties for each test function.
6979

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
7281

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.
7586

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
8092
```
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

Comments
 (0)