Add a new public function to invoke a list of tests.
This new function will call exit() with the correct value, depending on the result of the test run.
diff --git a/README.md b/README.md
index 1567893..e9070a6 100644
--- a/README.md
+++ b/README.md
@@ -47,13 +47,12 @@
}
```
-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:
+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 a test cases that you wish to run. For example:
```
-TestNSString().invokeTest()
-TestNSArray().invokeTest()
-TestNSDictionary().invokeTest()
-// ...
+XCTMain([TestNSString, TestNSArray, TestNSDictionary])
```
+The `XCTMain` function does not return, and will cause your test app to exit with either `0` for success or `1` for failure.
+
We are currently investigating ideas on how to make these additional steps for test discovery automatic when running on the Swift runtime.
diff --git a/XCTest/XCTest.swift b/XCTest/XCTest.swift
index 310a268..7255487 100644
--- a/XCTest/XCTest.swift
+++ b/XCTest/XCTest.swift
@@ -38,7 +38,6 @@
}
public func invokeTest() {
- XCTRun.registerExitHandler()
let tests = self.allTests
var totalDuration = 0.0
var totalFailures = 0
@@ -105,7 +104,21 @@
}
}
-internal func _XCTestPrintSummary() {
+internal struct XCTRun {
+ var duration: Double
+ var method: String
+ var passed: Bool
+ var failures: [XCTFailure]
+}
+
+/// Starts a test run for the specified test cases.
+///
+/// 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)`.
+/// - Parameter testCases: An array of test cases to run.
+@noreturn public func XCTMain(testCases: [XCTestCase]) {
+ for testCase in testCases {
+ testCase.invokeTest()
+ }
let (totalDuration, totalFailures) = XCTAllRuns.reduce((0.0, 0)) { ($0.0 + $1.duration, $0.1 + $1.failures.count) }
var testCountSuffix = "s"
@@ -118,23 +131,7 @@
}
let averageDuration = totalDuration / Double(XCTAllRuns.count)
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")
-
-}
-
-struct XCTRun {
- var duration: Double
- var method: String
- var passed: Bool
- var failures: [XCTFailure]
-
- static var registeredHandler = false
- static func registerExitHandler() {
- if registeredHandler {
- return
- }
- atexit(_XCTestPrintSummary)
- registeredHandler = true
- }
+ exit(totalFailures > 0 ? 1 : 0)
}
struct XCTFailure {