Xcode 10和super.tearDown

标签 xcode xctest teardown

从Xcode 10.1(也许是10)开始,当我创建单元测试文件时,我没有调用super.tearDown()和super.setUp()。

我没有在发行说明中看到这种变化。

在文档中https://developer.apple.com/documentation/xctest/xctestcase/understanding_setup_and_teardown_for_test_methods仍然在这里。

所以我的问题还是应该编写super.tearDown()和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是模板方法,其顶层实现为空。

尽管行为没有改变,但省略对super的调用意味着,如果您创建一个具有多个级别的测试层次结构,则必须将其重新添加。

您什么时候可以拥有多个级别?有两种情况:

  • 当您想针对不同的场景重用相同的测试时。
  • 当您将XCTestCase子类化以创建自定义帮助程序时。

  • 这些并非每天都会发生。但是它们确实发生了。确定“我在这里需要它,但在那儿不需要它”是危险的。因此,我一直都会调用super

    关于Xcode 10和super.tearDown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53448538/

    相关文章:

    xcode - 更新到 Xcode 7.3/Swift 2.2 后计时器停止工作

    swift - 在 UICollectionViewCell 中设置 firebase 值时出现 Nil 可选解包错误

    ios - 理解为什么在函数内部实例化后使用弱释放存储的属性

    pytest - 在测试完成时运行的基本 pytest 拆解

    java - 使用 junit 框架进行 sahi 测试中的 SetUp/TearDown 方法

    ios - 无法将 WKWebView 添加到 Xcode 项目

    c - 缺少用于使用复杂分析编译 C 的 complex.h

    iOS 单元测试 : How to unit test firstResponderStatus

    ios - 在 Swift 中模拟具有泛型参数函数的类

    unit-testing - 在Grails单元测试中使用mockDomain后是否需要删除metaClass?