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

时间:2021-01-02 01:20:31

Since Xcode 7 we have a nice API for UI testing. Mostly I'm satisfied with it. The only concern is related to the speed.

从Xcode 7开始,我们有一个很好的UI测试API。大部分时间我都很满意。唯一的问题与速度有关。

In the beginning an ordinary UI test case (about 15 actions) ran approximately 25 seconds. Then I mocked networking completely. Now it takes 20 seconds. Considering the fact that the time is taken only by animations and a launch time (1 second or even less), I assume, there must be a way to speed it up.

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

4 个解决方案

#1


15  

Try setting this property when your UI tests run:

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

UIApplication.shared.keyWindow?.layer.speed = 100

Here's how I set it:

这是我如何设置它:

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

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

And in my UI tests:

在我的UI测试中:

class MyAppUITests: XCTestCase {

    // MARK: - SetUp / TearDown

    override func setUp() {
        super.setUp()

        let app = XCUIApplication()
        app.launchArguments = ["UITests"]
        app.launch()
    }
}

There's a few more handy tips in this blog post.

这篇博文中还有一些方便的提示。

#2


6  

Another possibility is to disable animations at all:

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

[UIView setAnimationsEnabled:NO];

Swift 3:

斯威夫特3:

UIView.setAnimationsEnabled(false)

#3


3  

Following @Mark answer, the Swift 3 version:

关注@Mark回答,Swift 3版本:

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

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

On you ui test file:

在你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()

#4


1  

Add it in didFinishLaunch

在didFinishLaunch中添加它

[UIApplication sharedApplication].keyWindow.layer.speed = 2;

The default value is 1, make it 2 to double its speed.

默认值为1,使其速度加倍2倍。

#1


15  

Try setting this property when your UI tests run:

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

UIApplication.shared.keyWindow?.layer.speed = 100

Here's how I set it:

这是我如何设置它:

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

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

And in my UI tests:

在我的UI测试中:

class MyAppUITests: XCTestCase {

    // MARK: - SetUp / TearDown

    override func setUp() {
        super.setUp()

        let app = XCUIApplication()
        app.launchArguments = ["UITests"]
        app.launch()
    }
}

There's a few more handy tips in this blog post.

这篇博文中还有一些方便的提示。

#2


6  

Another possibility is to disable animations at all:

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

[UIView setAnimationsEnabled:NO];

Swift 3:

斯威夫特3:

UIView.setAnimationsEnabled(false)

#3


3  

Following @Mark answer, the Swift 3 version:

关注@Mark回答,Swift 3版本:

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

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

On you ui test file:

在你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()

#4


1  

Add it in didFinishLaunch

在didFinishLaunch中添加它

[UIApplication sharedApplication].keyWindow.layer.speed = 2;

The default value is 1, make it 2 to double its speed.

默认值为1,使其速度加倍2倍。