如何加快Xcode中的UItesting用例?

由于Xcode 7,我们有一个很好的用于UItesting的API。 大部分我都很满意。 唯一的问题是与速度有关。

一开始一个普通的UItesting用例(约15个动作)跑了大约25秒 。 然后我完全嘲笑networking。 现在需要20秒 。 考虑到时间只能通过animation和发射时间(1秒甚至更less)来实现,所以我认为,必须有一种方法加快速度。

尝试运行UItesting时设置此属性:

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

以下是我如何设置它:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if ProcessInfo.processInfo.arguments.contains("UITests") { UIApplication.shared.keyWindow?.layer.speed = 100 } } 

而在我的UItesting中:

 class MyAppUITests: XCTestCase { // MARK: - SetUp / TearDown override func setUp() { super.setUp() let app = XCUIApplication() app.launchArguments = ["UITests"] app.launch() } } 

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

另一种可能性是完全禁用animation:

 [UIView setAnimationsEnabled:NO]; 

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

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if ProcessInfo.processInfo.arguments.contains("UITests") { UIApplication.shared.keyWindow?.layer.speed = 200 } } 

在你的uitesting文件中:

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

将它添加到didFinishLaunch中

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

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