Xcode 10 和 super.tearDown [英] Xcode 10 and super.tearDown

查看:16
本文介绍了Xcode 10 和 super.tearDown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从 Xcode 10.1(可能是 10)以来,当我创建单元测试文件时,我没有调用 super.tearDown() 和 super.setUp() .

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.

在文档https://developer.apple.com/documentation/xctest/xctestcase/理解_setup_and_teardown_for_test_methods 仍然存在.

所以我的问题是我还应该写 super.tearDown() 和 super.setUp() 吗?

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

推荐答案

对于 XCTestCase 的直接子类,没有调用 super.setUp() 的行为从未发生任何变化.这是因为 setUptearDown 是在顶层具有空实现的模板方法.

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.

虽然行为没有变化,但省略对 super 的调用意味着,如果您创建一个具有多个级别的测试层次结构,则必须将它们添加回来.

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:

  • 当您想为不同的场景重复使用相同的测试时.
  • 当您子类化 XCTestCase 以制作自定义帮助程序时.

这些不会每天都发生.但它们确实发生了.决定我在这里需要它,但我不需要它"是危险的.所以我会一直调用 super.

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.

这篇关于Xcode 10 和 super.tearDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆