Skip to content

Commit d611120

Browse files
authored
Expand parameterized testing documentation to mention try/await support and showcase helper pattern (#1133)
This expands a few places where we document parameterized testing APIs to mention `try`/`await` support and showcase a common pattern for sharing arguments between multiple tests. ### Modifications: - For each `@Test` macro which accepts arguments, mention that `try` and `await` are supported and that arguments are lazily evaluated. - In the "Implementing parameterized tests" article, add a new section titled "Pass the same arguments to multiple test functions" showcasing the pattern of extracting common arguments to a separate property. - Add a [`> Tip:` callout](https://www.swift.org/documentation/docc/other-formatting-options#Add-Notes-and-Other-Asides) within that new article section mentioning `try`/`await` support. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated. Fixes rdar://130929060
1 parent d5b5c59 commit d611120

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

Sources/Testing/Test+Macro.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ public macro Test<C>(
217217
/// - collection: A collection of values to pass to the associated test
218218
/// function.
219219
///
220-
/// During testing, the associated test function is called once for each element
221-
/// in `collection`.
220+
/// You can prefix the expression you pass to `collection` with `try` or `await`.
221+
/// The testing library evaluates the expression lazily only if it determines
222+
/// that the associated test will run. During testing, the testing library calls
223+
/// the associated test function once for each element in `collection`.
222224
///
223225
/// @Comment {
224226
/// - Bug: The testing library should support variadic generics.
@@ -270,7 +272,10 @@ extension Test {
270272
/// - collection1: A collection of values to pass to `testFunction`.
271273
/// - collection2: A second collection of values to pass to `testFunction`.
272274
///
273-
/// During testing, the associated test function is called once for each pair of
275+
/// You can prefix the expressions you pass to `collection1` or `collection2`
276+
/// with `try` or `await`. The testing library evaluates the expressions lazily
277+
/// only if it determines that the associated test will run. During testing, the
278+
/// testing library calls the associated test function once for each pair of
274279
/// elements in `collection1` and `collection2`.
275280
///
276281
/// @Comment {
@@ -298,7 +303,10 @@ public macro Test<C1, C2>(
298303
/// - collection1: A collection of values to pass to `testFunction`.
299304
/// - collection2: A second collection of values to pass to `testFunction`.
300305
///
301-
/// During testing, the associated test function is called once for each pair of
306+
/// You can prefix the expressions you pass to `collection1` or `collection2`
307+
/// with `try` or `await`. The testing library evaluates the expressions lazily
308+
/// only if it determines that the associated test will run. During testing, the
309+
/// testing library calls the associated test function once for each pair of
302310
/// elements in `collection1` and `collection2`.
303311
///
304312
/// @Comment {
@@ -324,8 +332,11 @@ public macro Test<C1, C2>(
324332
/// - zippedCollections: Two zipped collections of values to pass to
325333
/// `testFunction`.
326334
///
327-
/// During testing, the associated test function is called once for each element
328-
/// in `zippedCollections`.
335+
/// You can prefix the expression you pass to `zippedCollections` with `try` or
336+
/// `await`. The testing library evaluates the expression lazily only if it
337+
/// determines that the associated test will run. During testing, the testing
338+
/// library calls the associated test function once for each element in
339+
/// `zippedCollections`.
329340
///
330341
/// @Comment {
331342
/// - Bug: The testing library should support variadic generics.
@@ -352,8 +363,11 @@ public macro Test<C1, C2>(
352363
/// - zippedCollections: Two zipped collections of values to pass to
353364
/// `testFunction`.
354365
///
355-
/// During testing, the associated test function is called once for each element
356-
/// in `zippedCollections`.
366+
/// You can prefix the expression you pass to `zippedCollections` with `try` or
367+
/// `await`. The testing library evaluates the expression lazily only if it
368+
/// determines that the associated test will run. During testing, the testing
369+
/// library calls the associated test function once for each element in
370+
/// `zippedCollections`.
357371
///
358372
/// @Comment {
359373
/// - Bug: The testing library should support variadic generics.

Sources/Testing/Testing.docc/ParameterizedTesting.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,37 @@ func makeLargeOrder(count: Int) async throws {
101101
- Note: Very large ranges such as `0 ..< .max` may take an excessive amount of
102102
time to test, or may never complete due to resource constraints.
103103

104+
### Pass the same arguments to multiple test functions
105+
106+
If you want to pass the same collection of arguments to two or more
107+
parameterized test functions, you can extract the arguments to a separate
108+
function or property and pass it to each `@Test` attribute. For example:
109+
110+
```swift
111+
extension Food {
112+
static var bestSelling: [Food] {
113+
get async throws { /* ... */ }
114+
}
115+
}
116+
117+
@Test(arguments: try await Food.bestSelling)
118+
func `Order entree`(food: Food) {
119+
let foodTruck = FoodTruck()
120+
#expect(foodTruck.order(food))
121+
}
122+
123+
@Test(arguments: try await Food.bestSelling)
124+
func `Package leftovers`(food: Food) throws {
125+
let foodTruck = FoodTruck()
126+
let container = try #require(foodTruck.container(fitting: food))
127+
try container.add(food)
128+
}
129+
```
130+
131+
> Tip: You can prefix expressions passed to `arguments:` with `try` or `await`.
132+
> The testing library evaluates them lazily only if it determines that the
133+
> associated test will run.
134+
104135
### Test with more than one collection
105136

106137
It's possible to test more than one collection. Consider the following test

0 commit comments

Comments
 (0)