Skip to content

Ensure that when .serialized is applied to a parameterized @Test func, its test cases are serialized #1188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/Testing/Traits/ParallelizationTrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public struct ParallelizationTrait: TestTrait, SuiteTrait {}
// MARK: - TestScoping

extension ParallelizationTrait: TestScoping {
public func scopeProvider(for test: Test, testCase: Test.Case?) -> Self? {
// When applied to a test function, this trait should provide scope to the
// test function itself, not its individual test cases, since that allows
// Runner to correctly interpret the configuration setting to disable
// parallelization.
test.isSuite || testCase == nil ? self : nil
}

public func provideScope(for test: Test, testCase: Test.Case?, performing function: @Sendable () async throws -> Void) async throws {
guard var configuration = Configuration.current else {
throw SystemError(description: "There is no current Configuration when attempting to provide scope for test '\(test.name)'. Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new")
Expand Down
21 changes: 19 additions & 2 deletions Tests/TestingTests/TestSupport/TestingAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ extension Runner {
/// - fileID: The `#fileID` string whose module should be used to locate
/// the test function to run.
/// - configuration: The configuration to use for running.
init(
selecting testName: String,
inModuleOf fileID: String = #fileID,
configuration: Configuration = .init()
) async {
let plan = await Runner.Plan(selecting: testName, inModuleOf: fileID, configuration: configuration)
self.init(plan: plan, configuration: configuration)
}
}

extension Runner.Plan {
/// Initialize an instance of this type that selects the free test function
/// named `testName` in the module specified in `fileID`.
///
/// - Parameters:
/// - testName: The name of the test function this instance should run.
/// - fileID: The `#fileID` string whose module should be used to locate
/// the test function to run.
/// - configuration: The configuration to use for running.
init(
selecting testName: String,
inModuleOf fileID: String = #fileID,
Expand All @@ -116,9 +135,7 @@ extension Runner {

await self.init(configuration: configuration)
}
}

extension Runner.Plan {
/// Initialize an instance of this type with the specified suite type.
///
/// - Parameters:
Expand Down
13 changes: 10 additions & 3 deletions Tests/TestingTests/Traits/ParallelizationTraitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

@Suite("Parallelization Trait Tests", .tags(.traitRelated))
struct ParallelizationTraitTests {
@Test(".serialized trait serializes parameterized test")
func serializesParameterizedTestFunction() async {
@Test(".serialized trait serializes parameterized test", arguments: await [
Runner.Plan(selecting: OuterSuite.self),
Runner.Plan(selecting: "globalParameterized(i:)"),
])
func serializesParameterizedTestFunction(plan: Runner.Plan) async {
var configuration = Configuration()
configuration.isParallelizationEnabled = true

Expand All @@ -33,7 +36,6 @@ struct ParallelizationTraitTests {
}
}

let plan = await Runner.Plan(selecting: OuterSuite.self, configuration: configuration)
let runner = Runner(plan: plan, configuration: configuration)
await runner.run()

Expand All @@ -59,3 +61,8 @@ private struct OuterSuite {
}
}
}

@Test(.hidden, .serialized, arguments: 0 ..< 10_000)
private func globalParameterized(i: Int) {
Issue.record("PARAMETERIZED\(i)")
}