Skip to content

Commit 9ce6d58

Browse files
committed
Implement XCTestCase.addTeardownBlock
Seems that this API was missing from the open source implementation. Includes tests.
1 parent 3675a01 commit 9ce6d58

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

Sources/XCTest/Public/XCTestCase.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ open class XCTestCase: XCTest {
132132
expected: false)
133133
}
134134
}
135+
runTeardownBlocks()
135136
tearDown()
136137
}
137138

@@ -178,6 +179,29 @@ open class XCTestCase: XCTest {
178179
/// class.
179180
open class func tearDown() {}
180181

182+
private var teardownBlocks: [() -> Void] = []
183+
private let teardownBlocksQueue: DispatchQueue = DispatchQueue(label: "org.swift.XCTest.XCTestCase.teardownBlocks")
184+
185+
/// Registers a block of teardown code to be run after the current test
186+
/// method ends.
187+
open func addTeardownBlock(_ block: @escaping () -> Void) {
188+
teardownBlocksQueue.async {
189+
self.teardownBlocks.append(block)
190+
}
191+
}
192+
193+
private func runTeardownBlocks() {
194+
let blocks = teardownBlocksQueue.sync { () -> [() -> Void] in
195+
let blocks = self.teardownBlocks
196+
self.teardownBlocks = []
197+
return blocks
198+
}
199+
200+
for block in blocks.reversed() {
201+
block()
202+
}
203+
}
204+
181205
open var continueAfterFailure: Bool {
182206
get {
183207
return true

Tests/Functional/TestCaseLifecycle/main.swift

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,62 @@ class NewInstanceForEachTestTestCase: XCTestCase {
8686
// CHECK: \t Executed 2 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
8787

8888

89+
// CHECK: Test Suite 'TeardownBlocksTestCase' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
90+
class TeardownBlocksTestCase: XCTestCase {
91+
static var allTests = {
92+
return [
93+
("test_withoutTeardownBlocks", test_withoutTeardownBlocks),
94+
("test_withATeardownBlock", test_withATeardownBlock),
95+
("test_withSeveralTeardownBlocks", test_withSeveralTeardownBlocks),
96+
]
97+
}()
98+
99+
override func tearDown() {
100+
print("In tearDown function")
101+
}
102+
103+
// CHECK: Test Case 'TeardownBlocksTestCase.test_withoutTeardownBlocks' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
104+
// CHECK: In tearDown function
105+
// CHECK: Test Case 'TeardownBlocksTestCase.test_withoutTeardownBlocks' passed \(\d+\.\d+ seconds\)
106+
func test_withoutTeardownBlocks() {
107+
// Intentionally blank
108+
}
109+
110+
// CHECK: Test Case 'TeardownBlocksTestCase.test_withATeardownBlock' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
111+
// CHECK: In teardown block A
112+
// CHECK: In tearDown function
113+
// CHECK: Test Case 'TeardownBlocksTestCase.test_withATeardownBlock' passed \(\d+\.\d+ seconds\)
114+
func test_withATeardownBlock() {
115+
addTeardownBlock {
116+
print("In teardown block A")
117+
}
118+
}
119+
120+
// CHECK: Test Case 'TeardownBlocksTestCase.test_withSeveralTeardownBlocks' started at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
121+
// CHECK: In teardown block C
122+
// CHECK: In teardown block B
123+
// CHECK: In tearDown function
124+
// CHECK: Test Case 'TeardownBlocksTestCase.test_withSeveralTeardownBlocks' passed \(\d+\.\d+ seconds\)
125+
func test_withSeveralTeardownBlocks() {
126+
addTeardownBlock {
127+
print("In teardown block B")
128+
}
129+
addTeardownBlock {
130+
print("In teardown block C")
131+
}
132+
}
133+
}
134+
// CHECK: Test Suite 'TeardownBlocksTestCase' passed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
135+
// CHECK: \t Executed 3 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
136+
137+
89138
XCTMain([
90139
testCase(SetUpTearDownTestCase.allTests),
91-
testCase(NewInstanceForEachTestTestCase.allTests)
140+
testCase(NewInstanceForEachTestTestCase.allTests),
141+
testCase(TeardownBlocksTestCase.allTests),
92142
])
93143

94144
// CHECK: Test Suite '.*\.xctest' passed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
95-
// CHECK: \t Executed 3 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
145+
// CHECK: \t Executed 6 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
96146
// CHECK: Test Suite 'All tests' passed at \d+-\d+-\d+ \d+:\d+:\d+\.\d+
97-
// CHECK: \t Executed 3 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
147+
// CHECK: \t Executed 6 tests, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

0 commit comments

Comments
 (0)