Swift Testing has a test trait you can apply to a suite or parameterized test called .serialized:
@Suite(.serialized) struct TerminalTests {
@Test func `Xterm color emulation`() {
Environment.set("TERM", to: "xterm-256")
#expect(Terminal.isColorEnabled)
}
@Test func `VT-100 emulation`() {
Environment.set("TERM", to: "vt100")
#expect(!Terminal.isColorEnabled)
}
}
In this example, because the suite has been annotated .serialized, the two tests function run one after the other. However, they can still run in parallel with other unrelated tests, including tests in other suites that are also marked .serialized:
@Suite(.serialized) struct TerminalTests {
// ...
}
@Suite(.serialized) struct CashRegisterTests {
@Test func `Add money to the register`() {
// ...
}
@Test func `Remove money to the register`() {
// ...
}
}
The intent here is to allow test authors to use .serialized while still maximizing parallelization.
I propose adding an overload of .serialized that lets test authors specify a data dependency. If a test is serialized for a given data dependency, then all other tests with the same dependency are also serialized with respect to that test.
We've received feedback that test authors expect the existing .serialized trait to serialize all tests it is applied to (regardless of which suites they are in). So I also propose changing its behaviour to do exactly that.
For the full proposal, click here.
Trying it out
To try this feature out, add a dependency to the main branch of swift-testing to your project:
...
dependencies: [
...
.package(url: "https://github.com/swiftlang/swift-testing.git", branch: "main"),
]
Then, add a target dependency to your test target:
.testTarget(
...
dependencies: [
...
.product(name: "Testing", package: "swift-testing"),
]
)
Finally, import Swift Testing using @_spi(Experimental) import Testing.