Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 10 and super.tearDown

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.  
        }  
    }  
}  
like image 987
Andrii Kuzminskyi Avatar asked Nov 23 '18 14:11

Andrii Kuzminskyi


People also ask

What is the use of setUp () and tearDown ()?

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.

What are setUp () and tearDown () methods in mobile app testing?

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.

What does tearDown function do?

tearDown()Provides an opportunity to perform cleanup after each test method in a test case ends.

What is XCTestCase?

The primary class for defining test cases, test methods, and performance tests. Xcode 7.2+


1 Answers

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:

  • When you want to reuse the same tests for different scenarios.
  • When you subclass XCTestCase to make a customized helper.

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.

like image 141
Jon Reid Avatar answered Sep 25 '22 07:09

Jon Reid