2017-07-27 110 views
0

我使用的是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

回答

1

你可以做這樣的事情

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

0

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