Skip to content

Add a new public function to invoke a list of tests. #1

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 2 commits into from
Nov 30, 2015
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
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,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