【问题标题】:How to run swift XCTest tearDown once如何运行 swift XCTest tearDown 一次
【发布时间】:2018-01-03 09:31:29
【问题描述】:

我正在使用 xcode 8.3.3,swift,我试图让 tearDown 方法只运行一次。

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

在 tearDown 方法中,我想注销应用程序。我只想做一次。

XCTest 文档有一个类 tearDown() 方法,但是当我尝试使用它时 - 它无法再访问应用程序了?: https://developer.apple.com/documentation/xctest/xctestcase/understanding_setup_and_teardown_for_test_methods

这是我在 tearDown 方法中得到的所有内容,因此它无法再访问应用程序上的任何元素:

如何在所有测试结束时只运行一次 tearDown 中的代码?

【问题讨论】:

  • 您的意思是在所有XCTestCases 中的所有测试结束时还是在当前XCTestCase 中的测试结束时?
  • 在当前 XCTestCase 中的所有测试之后,感谢您的澄清。

标签: swift xctest xcode-ui-testing ios-ui-automation xctestcase


【解决方案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() {}
}

【讨论】:

  • 谢谢!这个解决方法现在可以了 :) 我真的希望苹果能提供像他们说的那样工作的整体测试 setUp 和 tearDown 方法!我需要做的唯一更改是使用“self.testInvocations().count”而不是“testInvocations.count”
  • 我认为您不需要 self 部分,但在 swift 3 中可能仍需要 ()
【解决方案2】:

XCTestCase 有两种不同的 setUp/tearDown 组合。一种是在单个测试用例级别。另一个在套房级别。只需覆盖 class 版本即可获得整个套件:

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

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

【讨论】:

  • 嗨,我在问题中解释了为什么类 func 对我不起作用。
猜你喜欢
  • 2014-08-06
  • 2015-07-01
  • 1970-01-01
  • 2013-01-26
  • 2015-09-20
  • 2014-08-21
  • 2020-10-24
  • 2015-06-17
  • 1970-01-01
相关资源
最近更新 更多