As soon as UI testing was announced as a new feature in Xcode 7, we were itching to download the beta to start putting UI testing to the, uh, test. Starting a fresh project and targeting the latest (albeit, unreleased) version of iOS is one thing. But as we can see by Apple's continued support of the-phone-that-will-not-die, a.k.a. the iPhone 4S, the practical way to kick the tires of Apple's UI testing is to get it up and running in an app that's further along the development cycle than clicking single-view-application.
Good thing we did too, as we noticed a hiccup or two in setting up UI testing for an existing iOS app. Follow along and you'll see what we mean.
1. Convert to the latest Swift syntax #
If this is the first time you've opened this app in Xcode 7, you'll be prompted to convert your existing Swift code from 1.x to 2. Since you're safely on a different branch (you are, aren't you?), go ahead and hit convert.
You'll be prompted to pick the targets you want to convert. Choose at least your target and any existing tests you have and decide if you want to convert any imported frameworks. (For the record, we unchecked all CocoaPod-managed frameworks.)
2. Create a new UI testing target #
Go to File → New → Target and click Test under the iOS template categories, and select Cocoa Touch UI Testing Bundle.
3. Verify Enable Testability is set to “Yes” #
If you need to set up assertions on your app's model to see state change when running your UI tests, or check a UI item's accessibility identifier to make sure the appropriate element is on screen, you'll need to use the new @testable import to import your main app module (usually as @testable import YourAppName). If you don't have “Enable Testability” enabled (which, if your project is coming from Xcode 6 or earlier, it most likely doesn't as it's a new Build Option in Xcode 7), and you drop in your @testable import statement, you'll get a warning and your tests will fail to build and run.
So, head over to your app target, jump into Build Settings and set Enable Testability to Yes.
4. If you're using CocoaPods ... (if not, skip this step) #
You can also skip this step if you set up your Podfile with separate statements for importing libraries into your test targets (which, if you are, this is a reminder to do the same for your new UI test target). Otherwise, in your project's info tab, set your debug configuration to “Pods.debug” for your UI tests target if any of your tests have to interact with any of your CocoaPod libraries.
5. That's it! #
Jump into your AppName_UI_Tests.swift file, make your test function, and tap the record button to start making your first UI test!
Join 20,000+ Other Readers
Sign up to be notified of new blog posts and be the first to receive helpful app goodies from Savvy.