Since Xcode 10.1(maybe 10) when I create a Unit test file I don't have calls super.tearDown() and super.setUp() .
I've not seen such changes in release notes.
In documentation https://developer.apple.com/documentation/xctest/xctestcase/understanding_setup_and_teardown_for_test_methods are still here.
So my question should I still write super.tearDown() and super.setUp()?
class SomethingTests: XCTestCase {
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}
When a setUp() method is defined, the test runner will run that method prior to each test. Likewise, if a tearDown() method is defined, the test runner will invoke that method after each test.
setUp() — This method is called before the invocation of each test method in the given class. tearDown() — This method is called after the invocation of each test method in given class.
tearDown()Provides an opportunity to perform cleanup after each test method in a test case ends.
The primary class for defining test cases, test methods, and performance tests. Xcode 7.2+
For a direct subclass of XCTestCase, there never was any change of behavior for not calling super.setUp()
. That's because setUp
and tearDown
are template methods with empty implementations at the top level.
Though there's no change in behavior, omitting the calls to super
means that if you create a test hierarchy with more than one level, you'll have to add them back.
When would you ever have more than one level? There are two cases:
These don't happen every day. But they do happen. Deciding "I need it here, but I don't need it there" is perilous. So I'd just call super
all the time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With