Skip to content

Commit 198ec7e

Browse files
HugoSaymbrandonw
andauthored
Document parameterized tests isolation (#173)
* Document parameterized tests potential issues and fix * clean up --------- Co-authored-by: Brandon Williams <[email protected]>
1 parent 97f6ffb commit 198ec7e

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 5 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/Sharing/Documentation.docc/Articles/Testing.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ The same holds true for the [`fileStorage`](<doc:SharedReaderKey/fileStorage(_:d
4949
and [`inMemory`](<doc:SharedReaderKey/inMemory(_:)>) strategies. Even though those strategies do
5050
interact with a global store of data, they do so in a way that is quarantined from other tests.
5151

52+
### Repeated test runs and parameterized tests
53+
54+
Parallel testing of shared state typically works immediately without any changes to your application
55+
code or test code, but there are a few edge cases to be aware of. Running a test repeatedly or
56+
using parameterized tests require an additional step to make sure the shared state is quarantined
57+
from the multiple runes.
58+
59+
First you must import the DependenciesTestSupport library into your tests:
60+
61+
```swift
62+
import DependenciesTestSupport
63+
```
64+
65+
> Note: Our [Dependencies] library is used behind the scenes to control dependencies that power
66+
the various sharing strategies.
67+
68+
[Dependencies]: http://github.com/pointfreeco/swift-dependencies
69+
70+
Next you must apply the `.dependencies` trait to either your entire suite or the test you are
71+
running repeatedly:
72+
73+
```swift
74+
@Test(.dependencies)
75+
func increment() {
76+
// ...
77+
}
78+
```
79+
5280
### Testing when using custom persistence strategies
5381

5482
When creating your own custom persistence strategies you must be careful to do so in a style that
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import DependenciesTestSupport
2+
import Sharing
3+
import Testing
4+
5+
@Suite(.dependencies)
6+
struct MyTests {
7+
@Test(arguments: 0...10)
8+
func testCounterWithArguments(initialValue: Int) async throws {
9+
@Shared(.inMemory("count")) var counter = initialValue
10+
#expect(counter == initialValue)
11+
}
12+
}

0 commit comments

Comments
 (0)