forked from apple/swift-corelibs-xctest
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use XCTestRun to execute tests and report results
A major goal for swift-corelibs-xctest is API parity with Apple XCTest. This adds the largest missing API in swift-corelibs-xctest: `XCTestRun`. In Apple XCTest, `XCTestRun` is responsible for keeping track of the result of a test run. It's an integral part of how Apple XCTest works. swift-corelibs-xctest, on the other hand, used a global array of `XCTRun` structs to keep track of how many tests passed/failed. While it may have been possible to tack on `XCTestRun` to the swift-corelibs-xctest mechanism for failure reporting, this commit instead fully integrates it. As a result, the changes are widespread: gone is `XCTFailureHandler`, `XCTRun`, and other internal structures. In their place, welcome the Apple XCTest public APIs: the `XCTest` abstract class, `XCTestRun`, and its subclasses `XCTestCaseRun` and `XCTestSuiteRun`. In conjunction with the new `XCTestSuite`-related observation methods from apple#84, test reporting is now done exclusively through `XCTestObservation`. As a result, test output is now nearly identical to Apple XCTest.
- Loading branch information
Showing
26 changed files
with
824 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2016 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
// | ||
// PrintObserver.swift | ||
// Prints test progress to stdout. | ||
// | ||
|
||
#if os(Linux) || os(FreeBSD) | ||
import Foundation | ||
#else | ||
import SwiftFoundation | ||
#endif | ||
|
||
/// Prints textual representations of each XCTestObservation event to stdout. | ||
/// Mirrors the Apple XCTest output exactly. | ||
internal class PrintObserver: XCTestObservation { | ||
func testBundleWillStart(_ testBundle: NSBundle) {} | ||
|
||
func testSuiteWillStart(_ testSuite: XCTestSuite) { | ||
printAndFlush("Test Suite '\(testSuite.name)' started at \(dateFormatter.stringFromDate(testSuite.testRun!.startDate!))") | ||
} | ||
|
||
func testCaseWillStart(_ testCase: XCTestCase) { | ||
printAndFlush("Test Case '\(testCase.name)' started at \(dateFormatter.stringFromDate(testCase.testRun!.startDate!))") | ||
} | ||
|
||
func testCase(_ testCase: XCTestCase, didFailWithDescription description: String, inFile filePath: String?, atLine lineNumber: UInt) { | ||
let file = filePath ?? "<unknown>" | ||
printAndFlush("\(file):\(lineNumber): error: \(testCase.name) : \(description)") | ||
} | ||
|
||
func testCaseDidFinish(_ testCase: XCTestCase) { | ||
let testRun = testCase.testRun! | ||
let verb = testRun.hasSucceeded ? "passed" : "failed" | ||
// FIXME: Apple XCTest does not print a period after "(N seconds)". | ||
// The trailing period here should be removed and the functional | ||
// test suite should be updated. | ||
printAndFlush("Test Case '\(testCase.name)' \(verb) (\(formatTimeInterval(testRun.totalDuration)) seconds).") | ||
} | ||
|
||
func testSuiteDidFinish(_ testSuite: XCTestSuite) { | ||
let testRun = testSuite.testRun! | ||
let verb = testRun.hasSucceeded ? "passed" : "failed" | ||
printAndFlush("Test Suite '\(testSuite.name)' \(verb) at \(dateFormatter.stringFromDate(testRun.stopDate!))") | ||
|
||
let tests = testRun.executionCount == 1 ? "test" : "tests" | ||
let failures = testRun.totalFailureCount == 1 ? "failure" : "failures" | ||
printAndFlush( | ||
"\t Executed \(testRun.executionCount) \(tests), " + | ||
"with \(testRun.totalFailureCount) \(failures) (\(testRun.unexpectedExceptionCount) unexpected) " + | ||
"in \(formatTimeInterval(testRun.testDuration)) (\(formatTimeInterval(testRun.totalDuration))) seconds" | ||
) | ||
} | ||
|
||
func testBundleDidFinish(_ testBundle: NSBundle) {} | ||
|
||
private lazy var dateFormatter: NSDateFormatter = { | ||
let formatter = NSDateFormatter() | ||
formatter.dateFormat = "HH:mm:ss.SSS" | ||
return formatter | ||
}() | ||
|
||
private func printAndFlush(_ message: String) { | ||
print(message) | ||
fflush(stdout) | ||
} | ||
|
||
private func formatTimeInterval(_ timeInterval: NSTimeInterval) -> String { | ||
return String(round(timeInterval * 1000.0) / 1000.0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.