如何运行swift XCTest tearDown once

问题描述:

我使用的是xcode 8.3.3,swift,我试图让tearDown方法只运行一次。如何运行swift XCTest tearDown once

我在这里提供的解决方案启动应用程序一次: XCTestCase not launching application in setUp class method

在拆卸的方法,我想退出的应用程序。我只想做一次。

的XCTest文档中有一类拆解()方法,但是当我尝试使用它 - 它没有访问应用程序了?: https://developer.apple.com/documentation/xctest/xctestcase/understanding_setup_and_teardown_for_test_methods

这一切,当我在我得到拆机方法,因此它不能访问应用程序的任何内容了:

enter image description here

我怎么能在所有测试结束在拆机运行的代码只有一次?

+0

你的意思是,在整个所有'XCTestCase's或只是在当前'XCTestCase'试验结束时,所有的测试结束了吗? –

+0

经过当前XCTestCase的所有测试后,谢谢澄清。 – rfodge

你可以做这样的事情

import XCTest 

class TestSuite: XCTestCase { 

    static var testCount = testInvocations.count 

    override func setUp() 
    { 
     super.setUp() 

     TestSuite.testCount -= 1 
    } 

    override func tearDown() 
    { 
     if TestSuite.testCount == 0 { 
      print("Final tearDown") 
     } 

     super.tearDown() 
    } 

    func testA() {} 
    func testB() {} 
    func testC() {} 
} 
+0

谢谢!这个解决方法现在会做:)我真的希望苹果提供整体测试setUp和tearDown方法,就像他们说的那样工作!我需要做的唯一的改变是使用“self.testInvocations()。count”而不是“testInvocations.count” – rfodge

+0

我不认为你需要'self'部分,但'()'可能仍然需要在迅速3. –

XCTestCase有两种不同的安装/拆卸组合。一个是在个人测试案例级别。另一个是套件级别。只是覆盖class版本,以获得整个套件:

override class func setUp() { 
    super.setUp() 
    // Your code goes here 
} 

override class func tearDown() { 
    // Your code goes here 
    super.tearDown() 
} 
+0

嗨,我解释了为什么类func不适合我的问题。 – rfodge