Skip to content

Commit

Permalink
Merge pull request swiftlang#1 from parkera/master
Browse files Browse the repository at this point in the history
Add a new public function to invoke a list of tests.
  • Loading branch information
parkera committed Nov 30, 2015
2 parents 6ad307f + a107689 commit a6e2506
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ class TestNSURL : XCTestCase {
}
```

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 an array of instances of the 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.
35 changes: 16 additions & 19 deletions XCTest/XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ extension XCTestCase {
}

public func invokeTest() {
XCTRun.registerExitHandler()
let tests = self.allTests
var totalDuration = 0.0
var totalFailures = 0
Expand Down Expand Up @@ -105,7 +104,21 @@ extension XCTestCase {
}
}

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"
Expand All @@ -118,23 +131,7 @@ internal func _XCTestPrintSummary() {
}
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 {
Expand Down

0 comments on commit a6e2506

Please sign in to comment.