File tree Expand file tree Collapse file tree 4 files changed +53
-8
lines changed
Tests/Functional/TestCaseLifecycle Expand file tree Collapse file tree 4 files changed +53
-8
lines changed Original file line number Diff line number Diff line change @@ -129,6 +129,14 @@ public class XCTestCase: XCTest {
129
129
fatalError ( " Terminating execution due to test failure " )
130
130
}
131
131
}
132
+
133
+ /// Setup method called before the invocation of any test method in the
134
+ /// class.
135
+ public class func setUp( ) { }
136
+
137
+ /// Teardown method called after the invocation of every test method in the
138
+ /// class.
139
+ public class func tearDown( ) { }
132
140
}
133
141
134
142
/// Wrapper function allowing an array of static test case methods to fit
Original file line number Diff line number Diff line change 75
75
}
76
76
77
77
let filter = TestFiltering ( selectedTestName: selectedTestName)
78
- for (testCaseClass, testCaseMethods) in TestFiltering . filterTests ( testCases, filter: filter. selectedTestFilter) {
79
- let testCaseSuite = XCTestSuite ( name: " \( testCaseClass) " )
80
- for (testName, testClosure) in testCaseMethods {
81
- let testCase = testCaseClass. init ( name: testName, testClosure: testClosure)
82
- testCaseSuite. addTest ( testCase)
83
- }
84
- currentTestSuite. addTest ( testCaseSuite)
85
- }
78
+ TestFiltering . filterTests ( testCases, filter: filter. selectedTestFilter)
79
+ . map ( XCTestCaseSuite . init)
80
+ . forEach ( currentTestSuite. addTest)
86
81
87
82
rootTestSuite. run ( )
88
83
Original file line number Diff line number Diff line change @@ -68,3 +68,33 @@ public class XCTestSuite: XCTest {
68
68
tests. append ( test)
69
69
}
70
70
}
71
+
72
+ /// A test suite which is associated with a particular test case class. It will
73
+ /// call `setUp` and `tearDown` on the class itself before and after invoking
74
+ /// all of the test cases making up the class.
75
+ internal class XCTestCaseSuite : XCTestSuite {
76
+ private let testCaseClass : XCTestCase . Type ?
77
+
78
+ init ( testCaseEntry: XCTestCaseEntry ) {
79
+ let testCaseClass = testCaseEntry. 0
80
+ self . testCaseClass = testCaseClass
81
+ super. init ( name: String ( testCaseClass) )
82
+
83
+ for (testName, testClosure) in testCaseEntry. 1 {
84
+ let testCase = testCaseClass. init ( name: testName, testClosure: testClosure)
85
+ addTest ( testCase)
86
+ }
87
+ }
88
+
89
+ override func setUp( ) {
90
+ if let testCaseClass = testCaseClass {
91
+ testCaseClass. setUp ( )
92
+ }
93
+ }
94
+
95
+ override func tearDown( ) {
96
+ if let testCaseClass = testCaseClass {
97
+ testCaseClass. tearDown ( )
98
+ }
99
+ }
100
+ }
Original file line number Diff line number Diff line change @@ -21,6 +21,12 @@ class SetUpTearDownTestCase: XCTestCase {
21
21
22
22
var value = 0
23
23
24
+ // CHECK: In class setUp\(\)
25
+ override class func setUp( ) {
26
+ super. setUp ( )
27
+ print ( " In class \( #function) " )
28
+ }
29
+
24
30
override func setUp( ) {
25
31
super. setUp ( )
26
32
print ( " In \( #function) " )
@@ -41,6 +47,12 @@ class SetUpTearDownTestCase: XCTestCase {
41
47
print ( " In \( #function) " )
42
48
XCTAssertEqual ( value, 42 )
43
49
}
50
+
51
+ // CHECK: In class tearDown\(\)
52
+ override class func tearDown( ) {
53
+ super. tearDown ( )
54
+ print ( " In class \( #function) " )
55
+ }
44
56
}
45
57
// CHECK: Test Suite 'SetUpTearDownTestCase' passed at \d+:\d+:\d+\.\d+
46
58
// CHECK: \t Executed 1 test, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds
You can’t perform that action at this time.
0 commit comments