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
|
// RUN: %{swiftc} %s -o %T/TestCaseLifecycle
// RUN: %T/TestCaseLifecycle > %t || true
// RUN: %{xctest_checker} %t %s
#if os(macOS)
import SwiftXCTest
#else
import XCTest
#endif
// CHECK: Test Suite 'All tests' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Suite '.*\.xctest' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Suite 'SetUpTearDownTestCase' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
class SetUpTearDownTestCase: XCTestCase {
static var allTests = {
return [
("test_hasValueFromSetUp", test_hasValueFromSetUp),
]
}()
var value = 0
// CHECK: In class setUp\(\)
override class func setUp() {
super.setUp()
print("In class \(#function)")
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
}
override func setUp() {
super.setUp()
print("In \(#function)")
value = 42
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
}
override func tearDown() {
super.tearDown()
print("In \(#function)")
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
}
// CHECK: Test Case 'SetUpTearDownTestCase.test_hasValueFromSetUp' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: In setUp\(\)
// CHECK: In test_hasValueFromSetUp\(\)
// CHECK: In tearDown\(\)
// CHECK: Test Case 'SetUpTearDownTestCase.test_hasValueFromSetUp' passed \(\d+\.\d+ seconds\)
func test_hasValueFromSetUp() {
print("In \(#function)")
XCTAssertEqual(value, 42)
}
// CHECK: In class tearDown\(\)
override class func tearDown() {
super.tearDown()
print("In class \(#function)")
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
}
}
// CHECK: Test Suite 'SetUpTearDownTestCase' passed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 1 test, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: Test Suite 'NewInstanceForEachTestTestCase' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
class NewInstanceForEachTestTestCase: XCTestCase {
static var allTests = {
return [
("test_hasInitializedValue", test_hasInitializedValue),
("test_hasInitializedValueInAnotherTest", test_hasInitializedValueInAnotherTest),
]
}()
var value = 1
// CHECK: Test Case 'NewInstanceForEachTestTestCase.test_hasInitializedValue' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Case 'NewInstanceForEachTestTestCase.test_hasInitializedValue' passed \(\d+\.\d+ seconds\)
func test_hasInitializedValue() {
XCTAssertEqual(value, 1)
value += 1
}
// CHECK: Test Case 'NewInstanceForEachTestTestCase.test_hasInitializedValueInAnotherTest' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: Test Case 'NewInstanceForEachTestTestCase.test_hasInitializedValueInAnotherTest' passed \(\d+\.\d+ seconds\)
func test_hasInitializedValueInAnotherTest() {
XCTAssertEqual(value, 1)
}
}
// CHECK: Test Suite 'NewInstanceForEachTestTestCase' passed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 2 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: Test Suite 'TeardownBlocksTestCase' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
class TeardownBlocksTestCase: XCTestCase {
static var allTests = {
return [
("test_withoutTeardownBlocks", test_withoutTeardownBlocks),
("test_withATeardownBlock", test_withATeardownBlock),
("test_withSeveralTeardownBlocks", test_withSeveralTeardownBlocks),
]
}()
override func setUp() {
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
}
override func tearDown() {
print("In tearDown function")
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
}
// CHECK: Test Case 'TeardownBlocksTestCase.test_withoutTeardownBlocks' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: In tearDown function
// CHECK: Test Case 'TeardownBlocksTestCase.test_withoutTeardownBlocks' passed \(\d+\.\d+ seconds\)
func test_withoutTeardownBlocks() {
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
}
// CHECK: Test Case 'TeardownBlocksTestCase.test_withATeardownBlock' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: In teardown block A
// CHECK: In tearDown function
// CHECK: Test Case 'TeardownBlocksTestCase.test_withATeardownBlock' passed \(\d+\.\d+ seconds\)
func test_withATeardownBlock() {
addTeardownBlock {
print("In teardown block A")
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
}
}
// CHECK: Test Case 'TeardownBlocksTestCase.test_withSeveralTeardownBlocks' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: In teardown block C
// CHECK: In teardown block B
// CHECK: In tearDown function
// CHECK: Test Case 'TeardownBlocksTestCase.test_withSeveralTeardownBlocks' passed \(\d+\.\d+ seconds\)
func test_withSeveralTeardownBlocks() {
addTeardownBlock {
print("In teardown block B")
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
}
addTeardownBlock {
print("In teardown block C")
XCTAssert(Thread.isMainThread, "Expected to be ran on the main thread, but wasn't.")
}
}
}
// CHECK: Test Suite 'TeardownBlocksTestCase' passed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 3 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
XCTMain([
testCase(SetUpTearDownTestCase.allTests),
testCase(NewInstanceForEachTestTestCase.allTests),
testCase(TeardownBlocksTestCase.allTests),
])
// CHECK: Test Suite '.*\.xctest' passed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 6 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
// CHECK: Test Suite 'All tests' passed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
// CHECK: \t Executed 6 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
|