How to speed up UI test cases in Xcode?
Solution 1
Try setting this property when your UI tests run:
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:
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.
Solution 2
Another possibility is to disable animations at all:
[UIView setAnimationsEnabled:NO];
Swift 3:
UIView.setAnimationsEnabled(false)
Solution 3
Following @Mark answer, the Swift 3 version:
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:
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()
Solution 4
Add it in didFinishLaunch
[UIApplication sharedApplication].keyWindow.layer.speed = 2;
The default value is 1, make it 2 to double its speed.
Solution 5
I wanted to disable ALL animations during Snapshot testing. I was able to able to achieve this by disabling both Core Animation and UIView animations as below.
Note because my app used storyboards UIApplication.shared.keyWindow
was nil at launch so I access the UIWindow by referring to the window
property directly.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if ProcessInfo.processInfo.arguments.contains("SnapshotTests") {
// Disable Core Animations
window?.layer.speed = 0
// Disable UIView animations
UIView.setAnimationsEnabled(false)
}
return true
}
Related videos on Youtube
Artem Stepanenko
I’m an iOS developer who truly believes that the clean code exists.
Updated on June 04, 2022Comments
-
Artem Stepanenko almost 2 years
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.
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.
-
Artem Stepanenko about 8 yearsThanks for the answer. It works! In order to improve it a bit: is it possible to increase the animation speed from the UI testing process?
-
Mark about 8 yearsNo unfortunately. The ui test process is meant to be entirely separate from your app (and only interact through accessibility and the launch arguments).
-
Tomas Camin about 8 years@ArtemStepanenko you can increase the animation speed from the UI testing process by using SBTUITestTunnel. We developed this library to enable intercommunication between the app and test target.
-
Rudolf Adamkovič almost 8 yearsThis is awesome! Thank you so much for sharing. My test suite runs at the speed of light now.
-
Tomas Camin almost 8 yearsYou should not disable animations altogether as you might fail to catch some bugs specifically linked to animations. Check this great blog post for more info.
-
Noel Widmer almost 7 yearsCan you explain why your solution is better than the others?
-
Artem Stepanenko about 5 yearsThanks for sharing! I'm sure it took some time and effort. It would be even more useful for everyone if you read other answers first to make sure you don't repeat what others have already said. Cheers. Good job!
-
Mark almost 4 yearsJust an arbitrary value. 100 speeds it up enough for my needs.
-
lawicko over 3 yearsHi from 2020, this is a good answer and I started setting up my bluepill setup, however it would save me some time to know that parallel testing is now supported natively in Xcode, no need for 3rd party tools, at least of 1 machine setup.