Skip to content

Commit 69dc41b

Browse files
committed
Add class-level setUp and tearDown methods on XCTestCase
1 parent 0610ec3 commit 69dc41b

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

Sources/XCTest/XCTestCase.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ public class XCTestCase: XCTest {
129129
fatalError("Terminating execution due to test failure")
130130
}
131131
}
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() {}
132140
}
133141

134142
/// Wrapper function allowing an array of static test case methods to fit

Sources/XCTest/XCTestCaseSuite.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2016 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
//
10+
// XCTestCaseSuite.swift
11+
// A test suite associated with a paticular test case class.
12+
//
13+
14+
#if os(Linux) || os(FreeBSD)
15+
import Foundation
16+
#else
17+
import SwiftFoundation
18+
#endif
19+
20+
/// A test suite which is associated with a particular test case class. It will
21+
/// call `setUp` and `tearDown` on the class itself before and after invoking
22+
/// all of the test cases making up the class.
23+
internal class XCTestCaseSuite: XCTestSuite {
24+
private let testCaseClass: XCTestCase.Type?
25+
26+
init(testCaseEntry: XCTestCaseEntry) {
27+
let testCaseClass = testCaseEntry.testCaseClass
28+
self.testCaseClass = testCaseClass
29+
super.init(name: String(testCaseClass))
30+
31+
for (testName, testClosure) in testCaseEntry.allTests {
32+
let testCase = testCaseClass.init(name: testName, testClosure: testClosure)
33+
addTest(testCase)
34+
}
35+
}
36+
37+
override func setUp() {
38+
if let testCaseClass = testCaseClass {
39+
testCaseClass.setUp()
40+
}
41+
}
42+
43+
override func tearDown() {
44+
if let testCaseClass = testCaseClass {
45+
testCaseClass.tearDown()
46+
}
47+
}
48+
}

Sources/XCTest/XCTestMain.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,9 @@
7575
}
7676

7777
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)
8681

8782
rootTestSuite.run()
8883

Tests/Functional/TestCaseLifecycle/main.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class SetUpTearDownTestCase: XCTestCase {
2121

2222
var value = 0
2323

24+
// CHECK: In class setUp\(\)
25+
override class func setUp() {
26+
super.setUp()
27+
print("In class \(#function)")
28+
}
29+
2430
override func setUp() {
2531
super.setUp()
2632
print("In \(#function)")
@@ -41,6 +47,12 @@ class SetUpTearDownTestCase: XCTestCase {
4147
print("In \(#function)")
4248
XCTAssertEqual(value, 42)
4349
}
50+
51+
// CHECK: In class tearDown\(\)
52+
override class func tearDown() {
53+
super.tearDown()
54+
print("In class \(#function)")
55+
}
4456
}
4557
// CHECK: Test Suite 'SetUpTearDownTestCase' passed at \d+:\d+:\d+\.\d+
4658
// CHECK: \t Executed 1 test, with 0 failures \(0 unexpected\) in \d+\.\d+ \(\d+\.\d+\) seconds

XCTest.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
510957A91CA878410091D1A1 /* XCNotificationExpectationHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 510957A81CA878410091D1A1 /* XCNotificationExpectationHandler.swift */; };
11+
AE33888B1CD3D72500C39B1C /* XCTestCaseSuite.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE33888A1CD3D72500C39B1C /* XCTestCaseSuite.swift */; };
1112
AE7DD6091C8E81A0006FC722 /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE7DD6071C8E81A0006FC722 /* ArgumentParser.swift */; };
1213
AE7DD60A1C8E81A0006FC722 /* TestFiltering.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE7DD6081C8E81A0006FC722 /* TestFiltering.swift */; };
1314
AE7DD60C1C8F0513006FC722 /* XCTestObservation.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE7DD60B1C8F0513006FC722 /* XCTestObservation.swift */; };
@@ -41,6 +42,7 @@
4142
/* Begin PBXFileReference section */
4243
510957A81CA878410091D1A1 /* XCNotificationExpectationHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCNotificationExpectationHandler.swift; sourceTree = "<group>"; };
4344
5B5D86DB1BBC74AD00234F36 /* SwiftXCTest.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftXCTest.framework; sourceTree = BUILT_PRODUCTS_DIR; };
45+
AE33888A1CD3D72500C39B1C /* XCTestCaseSuite.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTestCaseSuite.swift; sourceTree = "<group>"; };
4446
AE7DD6061C8DC6C0006FC722 /* Functional */ = {isa = PBXFileReference; lastKnownFileType = folder; path = Functional; sourceTree = "<group>"; };
4547
AE7DD6071C8E81A0006FC722 /* ArgumentParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArgumentParser.swift; sourceTree = "<group>"; };
4648
AE7DD6081C8E81A0006FC722 /* TestFiltering.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestFiltering.swift; sourceTree = "<group>"; };
@@ -139,6 +141,7 @@
139141
AE9596E01C9692B8001A9EF0 /* XCTestObservationCenter.swift */,
140142
DACC94411C8B87B900EC85F5 /* XCWaitCompletionHandler.swift */,
141143
510957A81CA878410091D1A1 /* XCNotificationExpectationHandler.swift */,
144+
AE33888A1CD3D72500C39B1C /* XCTestCaseSuite.swift */,
142145
);
143146
path = XCTest;
144147
sourceTree = "<group>";
@@ -287,6 +290,7 @@
287290
AE9596DF1C96911F001A9EF0 /* ObjectWrapper.swift in Sources */,
288291
AED59FF61CB5394800F49260 /* XCTestErrors.swift in Sources */,
289292
C265F6721C3AEB6A00520CF9 /* XCTestMain.swift in Sources */,
293+
AE33888B1CD3D72500C39B1C /* XCTestCaseSuite.swift in Sources */,
290294
);
291295
runOnlyForDeploymentPostprocessing = 0;
292296
};

0 commit comments

Comments
 (0)