swift - 如何将私有(private)函数变成辅助函数?

标签 swift xcode xcode-ui-testing

我一直在为我的应用程序编写测试。但是,当运行我的测试时,我的函数上不断收到错误 Stall on main thread

private func waitForElementToAppear(testCase: XCTestCase,
                                    element: XCUIElement,
                                    file: String = #file,
                                    line: UInt = #line) {

    let existsPredicate = NSPredicate(format: "exists == true")
    testCase.expectationForPredicate(existsPredicate,
                                     evaluatedWithObject: element, handler: nil)

    testCase.waitForExpectationsWithTimeout(5) { (error) -> Void in
        if (error != nil) {
            let message = "Failed to find \(element) after 5 seconds."
            testCase.recordFailureWithDescription(message,
                                                  inFile: file, atLine: line, expected: true)
        }
    }
}

我在我的测试/代码中多次使用此函数。我怎样才能将其转换为辅助函数,这样我只需将该函数编写一次。提前感谢您的帮助:)

最佳答案

使用扩展更快捷:

extension XCTestCase {

    // your function here
}

因此您可以在所有测试类中使用它,而无需额外的类

关于swift - 如何将私有(private)函数变成辅助函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41810608/

相关文章:

ios - 为什么我收到错误 "fatal error: found nil while unwrapping optional value"?

ios - 如何在 Swift 5 中添加 ScrollView ?

ios - 当我尝试在设备上部署 PhoneGAP 应用程序时,xCode 返回 'timed out waiting for app to launch' 错误

ios - UI 测试中 Collection View 的错误单元格计数

Swift:从命令行运行时, Assets 对 UI 测试不可见

arrays - 在 swift 中使用 alamofire 发送 JSON 数组作为参数

ios - 防止释放函数中定义的对象

ios - SecondViewController 的prepareForSegue() 和 viewDidLoad()

xcode - macOS 上的系统 llvm-config 在哪里?

ios - 如何通过 UI 测试在 iOS13 共享表中找到要共享的应用程序?