Skip to content

Commit a6e2506

Browse files
committed
Merge pull request #1 from parkera/master
Add a new public function to invoke a list of tests.
2 parents 6ad307f + a107689 commit a6e2506

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ class TestNSURL : XCTestCase {
6363
}
6464
```
6565

66-
Also, this version of XCTest does not use the external test runner binary. Instead, create your own executable which links `libXCTest.so`. In your `main.swift`, list the test cases that you wish to run. For example:
66+
Also, this version of XCTest does not use the external test runner binary. Instead, create your own executable which links `libXCTest.so`. In your `main.swift`, invoke the `XCTMain` function with an array of instances of the test cases that you wish to run. For example:
6767

6868
```
69-
TestNSString().invokeTest()
70-
TestNSArray().invokeTest()
71-
TestNSDictionary().invokeTest()
72-
// ...
69+
XCTMain([TestNSString(), TestNSArray(), TestNSDictionary()])
7370
```
7471

72+
The `XCTMain` function does not return, and will cause your test app to exit with either `0` for success or `1` for failure.
73+
7574
We are currently investigating ideas on how to make these additional steps for test discovery automatic when running on the Swift runtime.

XCTest/XCTest.swift

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ extension XCTestCase {
3838
}
3939

4040
public func invokeTest() {
41-
XCTRun.registerExitHandler()
4241
let tests = self.allTests
4342
var totalDuration = 0.0
4443
var totalFailures = 0
@@ -105,7 +104,21 @@ extension XCTestCase {
105104
}
106105
}
107106

108-
internal func _XCTestPrintSummary() {
107+
internal struct XCTRun {
108+
var duration: Double
109+
var method: String
110+
var passed: Bool
111+
var failures: [XCTFailure]
112+
}
113+
114+
/// Starts a test run for the specified test cases.
115+
///
116+
/// This function will not return. If the test cases pass, then it will call `exit(0)`. If there is a failure, then it will call `exit(1)`.
117+
/// - Parameter testCases: An array of test cases to run.
118+
@noreturn public func XCTMain(testCases: [XCTestCase]) {
119+
for testCase in testCases {
120+
testCase.invokeTest()
121+
}
109122
let (totalDuration, totalFailures) = XCTAllRuns.reduce((0.0, 0)) { ($0.0 + $1.duration, $0.1 + $1.failures.count) }
110123

111124
var testCountSuffix = "s"
@@ -118,23 +131,7 @@ internal func _XCTestPrintSummary() {
118131
}
119132
let averageDuration = totalDuration / Double(XCTAllRuns.count)
120133
print("Total executed \(XCTAllRuns.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (0 unexpected) in \(round(averageDuration * 1000.0) / 1000.0) (\(round(totalDuration * 1000.0) / 1000.0)) seconds")
121-
122-
}
123-
124-
struct XCTRun {
125-
var duration: Double
126-
var method: String
127-
var passed: Bool
128-
var failures: [XCTFailure]
129-
130-
static var registeredHandler = false
131-
static func registerExitHandler() {
132-
if registeredHandler {
133-
return
134-
}
135-
atexit(_XCTestPrintSummary)
136-
registeredHandler = true
137-
}
134+
exit(totalFailures > 0 ? 1 : 0)
138135
}
139136

140137
struct XCTFailure {

0 commit comments

Comments
 (0)