如何在Xcode中加速UI测试用例?

Art*_*nko 17 objective-c ios xctest xcode7 uitest

Xcode 7开始,我们有一个很好的用于UI测试的API.大多数情况下我对它很满意.唯一的问题与速度有关.

在一开始,普通的UI测试用例(大约15个动作)运行大约25秒.然后我完全嘲笑网络.现在需要20秒.考虑到时间只取决于动画和启动时间(1秒甚至更短),我想,必须有办法加快速度.

Mar*_*ark 26

在UI测试运行时尝试设置此属性:

UIApplication.shared.keyWindow?.layer.speed = 100
Run Code Online (Sandbox Code Playgroud)

这是我如何设置它:

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if ProcessInfo.processInfo.arguments.contains("UITests") {
        UIApplication.shared.keyWindow?.layer.speed = 100
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的UI测试中:

class MyAppUITests: XCTestCase {

    // MARK: - SetUp / TearDown

    override func setUp() {
        super.setUp()

        let app = XCUIApplication()
        app.launchArguments = ["UITests"]
        app.launch()
    }
}
Run Code Online (Sandbox Code Playgroud)

这篇博客文章中有一些方便的提示.

  • @ArtemStepanenko 您可以使用 [SBTUITestTunnel](https://github.com/Subito-it/SBTUITestTunnel) 从 UI 测试过程中提高动画速度。我们开发了这个库来实现应用程序和测试目标之间的相互通信。 (2认同)

Art*_*nko 8

另一种可能性是禁用动画:

[UIView setAnimationsEnabled:NO];
Run Code Online (Sandbox Code Playgroud)

斯威夫特3:

UIView.setAnimationsEnabled(false)
Run Code Online (Sandbox Code Playgroud)

  • 您不应该完全禁用动画,因为您可能无法捕获与动画特定相关的一些错误.查看[好博客](https://pspdfkit.com/blog/2016/running-ui-tests-with-ludicrous-speed)帖子了解更多信息. (5认同)

mou*_*igo 6

在@Mark 回答之后,Swift 3版本:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if ProcessInfo.processInfo.arguments.contains("UITests") {
        UIApplication.shared.keyWindow?.layer.speed = 200
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的 ui 测试文件上:

override func setUp() {
    super.setUp()

    // Put setup code here. This method is called before the invocation of each test method in the class.

    let app = XCUIApplication()
    app.launchArguments = ["UITests"]
    app.launch()
Run Code Online (Sandbox Code Playgroud)