Xcode 10 and super.tearDown

♀尐吖头ヾ 提交于 2021-01-27 05:45:05

问题


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.  
        }  
    }  
}  

回答1:


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.



来源:https://stackoverflow.com/questions/53448538/xcode-10-and-super-teardown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!