Skip to content

Commit cd060ff

Browse files
authored
Execute filtered tests with single XCTest invocation (swiftlang#2479)
1 parent b25e390 commit cd060ff

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

IntegrationTests/Tests/IntegrationTests/BasicTests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,35 @@ final class BasicTests: XCTestCase {
201201
XCTAssertEqual(runOutput, "1 \"two\"\n")
202202
}
203203
}
204+
205+
func testSwiftTest() throws {
206+
try withTemporaryDirectory { dir in
207+
let toolDir = dir.appending(component: "swiftTest")
208+
try localFileSystem.createDirectory(toolDir)
209+
try sh(swiftPackage, "--package-path", toolDir, "init", "--type", "library")
210+
try localFileSystem.writeFileContents(
211+
toolDir.appending(components: "Tests", "swiftTestTests", "MyTests.swift"),
212+
bytes: ByteString(encodingAsUTF8: """
213+
import XCTest
214+
215+
final class MyTests: XCTestCase {
216+
func testFoo() {
217+
XCTAssertTrue(1 == 1)
218+
}
219+
func testBar() {
220+
XCTAssertFalse(1 == 2)
221+
}
222+
func testBaz() { }
223+
}
224+
"""))
225+
let testOutput = try sh(swiftTest, "--package-path", toolDir, "--filter", "MyTests.*").stderr
226+
227+
// Check the test log.
228+
XCTAssertContents(testOutput) { checker in
229+
checker.check(.contains("Test Suite 'MyTests' started"))
230+
checker.check(.contains("Test Suite 'MyTests' passed"))
231+
checker.check(.contains("Executed 3 tests, with 0 failures"))
232+
}
233+
}
234+
}
204235
}

Sources/Commands/SwiftTestTool.swift

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
196196
case .runSerial:
197197
let toolchain = try getToolchain()
198198
let testProducts = try buildTestsIfNeeded()
199-
var ranSuccessfully = true
200199
let buildParameters = try self.buildParameters()
201200

202201
// Clean out the code coverage directory that may contain stale
@@ -205,18 +204,11 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
205204
try localFileSystem.removeFileTree(buildParameters.codeCovPath)
206205
}
207206

207+
let xctestArg: String?
208+
208209
switch options.testCaseSpecifier {
209210
case .none:
210-
let runner = TestRunner(
211-
bundlePaths: testProducts.map { $0.bundlePath },
212-
xctestArg: nil,
213-
processSet: processSet,
214-
toolchain: toolchain,
215-
diagnostics: diagnostics,
216-
options: self.options,
217-
buildParameters: buildParameters
218-
)
219-
ranSuccessfully = runner.test()
211+
xctestArg = nil
220212

221213
case .regex, .specific, .skip:
222214
// If old specifier `-s` option was used, emit deprecation notice.
@@ -233,21 +225,21 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
233225
diagnostics.emit(.noMatchingTests)
234226
}
235227

236-
// Finally, run the tests.
237-
for test in tests {
238-
let runner = TestRunner(
239-
bundlePaths: [test.productPath],
240-
xctestArg: test.specifier,
241-
processSet: processSet,
242-
toolchain: toolchain,
243-
diagnostics: diagnostics,
244-
options: self.options,
245-
buildParameters: try self.buildParameters()
246-
)
247-
ranSuccessfully = runner.test() && ranSuccessfully
248-
}
228+
xctestArg = tests.map { $0.specifier }.joined(separator: ",")
249229
}
250230

231+
let runner = TestRunner(
232+
bundlePaths: testProducts.map { $0.bundlePath },
233+
xctestArg: xctestArg,
234+
processSet: processSet,
235+
toolchain: toolchain,
236+
diagnostics: diagnostics,
237+
options: self.options,
238+
buildParameters: buildParameters
239+
)
240+
241+
// Finally, run the tests.
242+
let ranSuccessfully: Bool = runner.test()
251243
if !ranSuccessfully {
252244
executionStatus = .failure
253245
}

0 commit comments

Comments
 (0)