UI Testing in Xcode 7
Preface: This blog does not contain screenshots because of Apple’s confidentiality agreement for the beta.
Xcode is more shitty at UI testing than you are at writing readable code (barely). It’s a serious problem that is addressed at pretty much every developer conference, and it’s never solved. So, yeah, the speakers are all wasting their and your time. You do want to keep reading my blog about UI testing in Xcode 7 though, things are DEFINITELY different now.
So go download the Beta. It’s gonna take a while. As it should. It’s huge and you didn’t contribute, so shut up and be grateful. When it’s finally done, open up one of your existing projects. Turn on accessibility for all elements you’re planning on testing. You’re then going to go to new, and then target. Then under iOS, select Cocoa Touch UI Testing bundle. Thankfully Apple knows how simple you are and designs their interface accordingly.
Go through all of the typical file making bs and then check the project navigator (that’s the side menu with the folders you neanderthal). You will see a folder for your tests. Open the folder. Click the file with UITest. Breathe.
Ok, now you can start testing. Actually, you’re probably scrolling up to reread the snail paced instructions you should have gotten the first time. I’m sorry about the lack of screenshots, I know words intimidate you. Catch up yet? Good. In the file, you’ll see a method that says testing example.
The prefix test lets Xcode infer that the method is one of your tests (if you didn’t figure that out, I guess Xcode is smarter than you. Don’t let that get you down, it only crashes like, 3 times a day). Click in the body of the provided example or use your outstanding creativity to name your own method, as long as you remember to prefix it with test (write it on your hand). Now notice the red record button in the debugger’s left pane (the bottom window that you wanted to dismiss but couldn’t figure out how). Pressing this will launch your app and have Xcode autogenerate code to simulate your fat fingers spreading grease on your screen.
After this code is generated, you can set a variable and call XCTAssert to check if it’s actually that value. Yes, you need to figure out what you want the value to be. For example, you know that remarkably innovative to-do list app you’re working on that’s gonna make everyone but the most hardheaded curmudgeon stop using Google Calendar? You can set an NSUInteger to equal the row count of your tableview, add an item in the game changing way you implemented in your app, and use XCTAssert to check that the row count was increased by one. Then run your test. And that’s it. You’re done. Woohoo.
Big thanks to Julio Rivera, whose presentation at Prolific interactive inspired this post, and to Adrian Lignos and Dimitri Dietz for helping me clean it up.