Skip to content

Commit a27e557

Browse files
authored
[6.1] Add an overview of test serialization to the XCTest migration guide (#965)
- **Explanation**: Updates the article on migrating tests from XCTest to tell developers about test serialization. - **Scope**: Documentation updates. - **Issues**: #863 - **Original PRs**: #960 #963 - **Risk**: Purely documentation updates, no risk to the release. - **Testing**: Build documentation and read Migrating a test from XCTest. - **Reviewers**: @grynspan @stmontgomery
1 parent 9e0b3c0 commit a27e557

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

Sources/Testing/Testing.docc/MigratingFromXCTest.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ with optional expressions to unwrap them:
273273

274274
### Record issues
275275

276-
Finally, XCTest has a function, [`XCTFail()`](https://developer.apple.com/documentation/xctest/1500970-xctfail),
276+
XCTest has a function, [`XCTFail()`](https://developer.apple.com/documentation/xctest/1500970-xctfail),
277277
that causes a test to fail immediately and unconditionally. This function is
278278
useful when the syntax of the language prevents the use of an `XCTAssert()`
279279
function. To record an unconditional issue using the testing library, use the
@@ -692,6 +692,56 @@ of issues:
692692
}
693693
}
694694

695+
### Run tests sequentially
696+
697+
By default, the testing library runs all tests in a suite in parallel. The
698+
default behavior of XCTest is to run each test in a suite sequentially. If your
699+
tests use shared state such as global variables, you may see unexpected
700+
behavior including unreliable test outcomes when you run tests in parallel.
701+
702+
Annotate your test suite with ``Trait/serialized`` to run tests within that
703+
suite serially:
704+
705+
@Row {
706+
@Column {
707+
```swift
708+
// Before
709+
class RefrigeratorTests : XCTestCase {
710+
func testLightComesOn() throws {
711+
try FoodTruck.shared.refrigerator.openDoor()
712+
XCTAssertEqual(FoodTruck.shared.refrigerator.lightState, .on)
713+
}
714+
715+
func testLightGoesOut() throws {
716+
try FoodTruck.shared.refrigerator.openDoor()
717+
try FoodTruck.shared.refrigerator.closeDoor()
718+
XCTAssertEqual(FoodTruck.shared.refrigerator.lightState, .off)
719+
}
720+
}
721+
```
722+
}
723+
@Column {
724+
```swift
725+
// After
726+
@Suite(.serialized)
727+
class RefrigeratorTests {
728+
@Test func lightComesOn() throws {
729+
try FoodTruck.shared.refrigerator.openDoor()
730+
#expect(FoodTruck.shared.refrigerator.lightState == .on)
731+
}
732+
733+
@Test func lightGoesOut() throws {
734+
try FoodTruck.shared.refrigerator.openDoor()
735+
try FoodTruck.shared.refrigerator.closeDoor()
736+
#expect(FoodTruck.shared.refrigerator.lightState == .off)
737+
}
738+
}
739+
```
740+
}
741+
}
742+
743+
For more information, see <doc:Parallelization>.
744+
695745
## See Also
696746

697747
- <doc:DefiningTests>

0 commit comments

Comments
 (0)