1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 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
//
//
// XCTestMain.swift
// This is the main file for the framework. It provides the entry point function
// for running tests and some infrastructure for running them.
//
// Note that we are re-exporting Foundation so tests importing XCTest don't need
// to import it themselves. This is consistent with the behavior of Apple XCTest
#if os(macOS)
#if USE_FOUNDATION_FRAMEWORK
@_exported import Foundation
#else
@_exported import SwiftFoundation
#endif
#else
@_exported import Foundation
#endif
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#endif
/// Starts a test run for the specified test cases.
///
/// Example usage:
///
/// class TestFoo: XCTestCase {
/// static var allTests = {
/// return [
/// ("test_foo", test_foo),
/// ("test_bar", test_bar),
/// ]
/// }()
///
/// func test_foo() {
/// // Test things...
/// }
///
/// // etc...
/// }
///
/// let exitCode = XCTMain([ testCase(TestFoo.allTests) ])
///
/// Command line arguments can be used to select a particular test case or class
/// to execute. For example:
///
/// ./FooTests FooTestCase/testFoo # Run a single test case
/// ./FooTests FooTestCase # Run all the tests in FooTestCase
///
/// - Parameters:
/// - testCases: An array of test cases run, each produced by a call to the
/// `testCase` function.
/// - arguments: Command-line arguments to pass to XCTest. By default, the
/// arguments passed to the process are used.
/// - observers: Zero or more observers that should observe events that
/// occur while testing. If `nil` (the default), events are written to
/// the console.
///
/// - Returns: The exit code to use when the process terminates. `EXIT_SUCCESS`
/// indicates success, while any other value (including `EXIT_FAILURE`)
/// indicates failure.
#if DISABLE_XCTWAITER
@_disfavoredOverload
public func XCTMain(
_ testCases: [XCTestCaseEntry],
arguments: [String] = CommandLine.arguments,
observers: [XCTestObservation]? = nil
) async -> CInt {
// Async-version of XCTMain()
switch XCTMainMisc(testCases, arguments: arguments, observers: observers) {
case .exitCode(let code):
return code
case .testSuite(let rootTestSuite, let testBundle, let observers):
// Add a test observer that prints test progress to stdout.
let observationCenter = XCTestObservationCenter.shared
for observer in observers {
observationCenter.addTestObserver(observer)
}
observationCenter.testBundleWillStart(testBundle)
await rootTestSuite._runAsync()
observationCenter.testBundleDidFinish(testBundle)
return rootTestSuite.testRun!.totalFailureCount == 0 ? EXIT_SUCCESS : EXIT_FAILURE
}
}
#else
@_disfavoredOverload
public func XCTMain(
_ testCases: [XCTestCaseEntry],
arguments: [String] = CommandLine.arguments,
observers: [XCTestObservation]? = nil
) -> CInt {
// Sync-version of XCTMain()
switch XCTMainMisc(testCases, arguments: arguments, observers: observers) {
case .exitCode(let code):
return code
case .testSuite(let rootTestSuite, let testBundle, let observers):
// Add a test observer that prints test progress to stdout.
let observationCenter = XCTestObservationCenter.shared
for observer in observers {
observationCenter.addTestObserver(observer)
}
observationCenter.testBundleWillStart(testBundle)
rootTestSuite.run()
observationCenter.testBundleDidFinish(testBundle)
return rootTestSuite.testRun!.totalFailureCount == 0 ? EXIT_SUCCESS : EXIT_FAILURE
}
}
#endif
internal enum TestSuiteOrExitCode {
case testSuite(rootTestSuite: XCTestSuite, testBundle: Bundle, observers: [XCTestObservation])
case exitCode(CInt)
}
/// Returns a test suite to be run or an exit code for the specified test cases and
/// command-line arguments.
internal func XCTMainMisc(
_ testCases: [XCTestCaseEntry],
arguments: [String] = CommandLine.arguments,
observers: [XCTestObservation]?
) -> TestSuiteOrExitCode {
let observers = observers ?? [PrintObserver()]
let testBundle = Bundle.main
let executionMode = ArgumentParser(arguments: arguments).executionMode
// Apple XCTest behaves differently if tests have been filtered:
// - The root `XCTestSuite` is named "Selected tests" instead of
// "All tests".
// - An `XCTestSuite` representing the .xctest test bundle is not included.
let rootTestSuite: XCTestSuite
let currentTestSuite: XCTestSuite
if executionMode.selectedTestNames == nil {
rootTestSuite = XCTestSuite(name: "All tests")
currentTestSuite = XCTestSuite(name: "\(testBundle.bundleURL.lastPathComponent).xctest")
rootTestSuite.addTest(currentTestSuite)
} else {
rootTestSuite = XCTestSuite(name: "Selected tests")
currentTestSuite = rootTestSuite
}
let filter = TestFiltering(selectedTestNames: executionMode.selectedTestNames)
TestFiltering.filterTests(testCases, filter: filter.selectedTestFilter)
.map(XCTestCaseSuite.init)
.forEach(currentTestSuite.addTest)
switch executionMode {
case .list(type: .humanReadable):
TestListing(testSuite: rootTestSuite).printTestList()
return .exitCode(EXIT_SUCCESS)
case .list(type: .json):
TestListing(testSuite: rootTestSuite).printTestJSON()
return .exitCode(EXIT_SUCCESS)
case let .help(invalidOption):
if let invalid = invalidOption {
let errMsg = "Error: Invalid option \"\(invalid)\"\n"
FileHandle.standardError.write(errMsg.data(using: .utf8) ?? Data())
}
let exeName = URL(fileURLWithPath: arguments[0]).lastPathComponent
let sampleTest = rootTestSuite.list().first ?? "Tests.FooTestCase/testFoo"
let sampleTests = sampleTest.prefix(while: { $0 != "/" })
print("""
Usage: \(exeName) [OPTION]
\(exeName) [TESTCASE]
Run and report results of test cases.
With no OPTION or TESTCASE, runs all test cases.
OPTIONS:
-l, --list-test List tests line by line to standard output
--dump-tests-json List tests in JSON to standard output
TESTCASES:
Run a single test
> \(exeName) \(sampleTest)
Run all the tests in \(sampleTests)
> \(exeName) \(sampleTests)
""")
return .exitCode(invalidOption == nil ? EXIT_SUCCESS : EXIT_FAILURE)
case .run(selectedTestNames: _):
return .testSuite(rootTestSuite: rootTestSuite, testBundle: testBundle, observers: observers)
}
}
#if DISABLE_XCTWAITER
// @available(*, deprecated, message: "Call the overload of XCTMain() that returns an exit code instead.")
public func XCTMain(_ testCases: [XCTestCaseEntry]) async -> Never {
exit(await XCTMain(testCases, arguments: CommandLine.arguments, observers: nil) as CInt)
}
// @available(*, deprecated, message: "Call the overload of XCTMain() that returns an exit code instead.")
public func XCTMain(_ testCases: [XCTestCaseEntry], arguments: [String]) async -> Never {
exit(await XCTMain(testCases, arguments: arguments, observers: nil) as CInt)
}
// @available(*, deprecated, message: "Call the overload of XCTMain() that returns an exit code instead.")
public func XCTMain(
_ testCases: [XCTestCaseEntry],
arguments: [String],
observers: [XCTestObservation]
) async -> Never {
exit(await XCTMain(testCases, arguments: arguments, observers: observers) as CInt)
}
#else
// @available(*, deprecated, message: "Call the overload of XCTMain() that returns an exit code instead.")
public func XCTMain(_ testCases: [XCTestCaseEntry]) -> Never {
exit(XCTMain(testCases, arguments: CommandLine.arguments, observers: nil) as CInt)
}
// @available(*, deprecated, message: "Call the overload of XCTMain() that returns an exit code instead.")
public func XCTMain(_ testCases: [XCTestCaseEntry], arguments: [String]) -> Never {
exit(XCTMain(testCases, arguments: arguments, observers: nil) as CInt)
}
// @available(*, deprecated, message: "Call the overload of XCTMain() that returns an exit code instead.")
public func XCTMain(
_ testCases: [XCTestCaseEntry],
arguments: [String],
observers: [XCTestObservation]
) -> Never {
exit(XCTMain(testCases, arguments: arguments, observers: observers) as CInt)
}
#endif
|