0001    // This source file is part of the Swift.org open source project
0002    //
0003    // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
0004    // Licensed under Apache License v2.0 with Runtime Library Exception
0005    //
0006    // See http://swift.org/LICENSE.txt for license information
0007    // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
0008    //
0009    //
0010    //  XCTestCase.swift
0011    //  Base protocol (and extension with default methods) for test cases
0012    //
0013    
0014    public protocol XCTestCase
XCTestCase.swift:19
extension XCTestCase {
XCTestMain.swift:52
@noreturn public func XCTMain(testCases: [XCTestCase]) {
: XCTestCaseProvider { 0015 func setUp
XCTestCase.swift:51
                setUp()
() 0016 func tearDown
XCTestCase.swift:62
                tearDown()
() 0017 } 0018 0019 extension XCTestCase { 0020 0021 public var continueAfterFailure
XCTestCase.swift:41
                    if !self.continueAfterFailure {
: Bool { 0022 get { 0023 return true 0024 } 0025 set { 0026 // TODO: When using the Objective-C runtime, XCTest is able to throw an exception from an assert and then catch it at the frame above the test method. This enables the framework to effectively stop all execution in the current test. There is no such facility in Swift. Until we figure out how to get a compatible behavior, we have decided to hard-code the value of 'true' for continue after failure. 0027 } 0028 } 0029 0030 public func invokeTest
XCTestMain.swift:55
            testCase.invokeTest()
() { 0031 let tests = self.allTests 0032 var totalDuration = 0.0 0033 var totalFailures = 0 0034 var unexpectedFailures = 0 0035 let overallDuration = measureTimeExecutingBlock { 0036 for (name, test) in tests { 0037 let method = "\(self.dynamicType).\(name)" 0038 0039 var failures = [XCTFailure]() 0040 XCTFailureHandler = { failure in 0041 if !self.continueAfterFailure { 0042 failure.emit(method) 0043 fatalError("Terminating execution due to test failure", file: failure.file, line: failure.line) 0044 } else { 0045 failures.append(failure) 0046 } 0047 } 0048 0049 XCTPrint("Test Case '\(method)' started.") 0050 0051 setUp() 0052 0053 let duration = measureTimeExecutingBlock { 0054 do { 0055 try test() 0056 } catch { 0057 let unexpectedFailure = XCTFailure(message: "", failureDescription: "threw error \"\(error)\"", expected: false, file: "<EXPR>", line: 0) 0058 XCTFailureHandler!(unexpectedFailure) 0059 } 0060 } 0061 0062 tearDown() 0063 0064 totalDuration += duration 0065 0066 var result = "passed" 0067 for failure in failures { 0068 failure.emit(method) 0069 totalFailures += 1 0070 if !failure.expected { 0071 unexpectedFailures += 1 0072 } 0073 result = failures.count > 0 ? "failed" : "passed" 0074 } 0075 0076 XCTPrint("Test Case '\(method)' \(result) (\(printableStringForTimeInterval(duration)) seconds).") 0077 XCTAllRuns.append(XCTRun(duration: duration, method: method, passed: failures.count == 0, failures: failures)) 0078 XCTFailureHandler = nil 0079 } 0080 } 0081 0082 var testCountSuffix = "s" 0083 if tests.count == 1 { 0084 testCountSuffix = "" 0085 } 0086 var failureSuffix = "s" 0087 if totalFailures == 1 { 0088 failureSuffix = "" 0089 } 0090 0091 XCTPrint("Executed \(tests.count) test\(testCountSuffix), with \(totalFailures) failure\(failureSuffix) (\(unexpectedFailures) unexpected) in \(printableStringForTimeInterval(totalDuration)) (\(printableStringForTimeInterval(overallDuration))) seconds") 0092 } 0093 0094 public func setUp() { 0095 0096 } 0097 0098 public func tearDown() { 0099 0100 } 0101 } 0102