The Xcode’s XCTest framework is getting used more and more by developers and QA folks that aim to test their iOS apps efficiently. This framework is a great addition to an iOS developer's toolbox and in order to get you up and running with it, here are some basic step-by-step instructions on how to get started with XCTest and how to create IPA properly for a test session.
Methods to Create iOS Package Archive
The application and test package are both required for XCTest testing. The same thing applies for any functional UI test automation and then the application (whether IPA or APK) can be used for target devices and test package for instrumentation or to be used as part of the device session.
Working on Xcode to Create IPA for Testing
First we’ll take a look at Xcode and how you should be working with it to produce IPA compatible with a device session and with the right configurations.
1. Archive Your Build.
When you archive your build, the package will be compatible with an iOS device. When your build is done and archiving has finished, select the build from Archive list and click “Export…”:
2. Select The Right Method for Export.
When the following window is shown, simply select “Save for Ad Hoc Deployment” and click Next.
3. Identify Yourself (and the Build).
Use the same identify what you use in build settings for code signing. If your project and Xcode is properly configured you should see the following type of dialog proposing the first usable identifier:
4. Select Supported OS and Devices.
It’s almost always recommended to include all possible device support for your app. However, if you want to reduce the size of your IPA you can shrink it by selecting to support only certain devices and OS versions. In that case, you would select Export for specific devices and select devices from the dropdown. To support all of them (recommended), just select “Export one app for all compatible devices”:
All done! Now just follow through the wizard to locate your .ipa file and everything will be ready for the test session.
Working on Command Line to Create IPA for Testing
To get started with command line script you first need to make sure IPA file will be testable and it can be packaged properly.
1. Select to Build for Testing.
This can be done Product -> Build for -> Testing menu:
2. Locate the App File on Your Harddisk .
Next, select your project in Project Navigator and right click to “Show it in Finder”:
3. Copy the Application File.
After Finder dialog has been opened and files are shown, just highlight the .app file and right-click to see copying option, as follows:
4. Create IPA from Command Line.
Finally, open the terminal, and create IPA package with following command line:
$ mkdir /ProjectName $ cd ProjectName $ cp -r /Path/To/Your/IPA/File/LocalizationDemo.app . $ cd .. $ zip --symlinks -qr "LocalizationDemo.ipa" ProjectName
Now you have an IPA file to be used with a test session on devices.
Create a Package for XCTest
Things will start with compiling unit tests. It will actually happen automatically if you have checked target properly. This can be done or just verified that everything is properly set up under Product -> Scheme -> Edit scheme.
Select Target on the left-hand side menu and check all items on Run column:
This will make sure your tests will be compiled whenever your app is getting built.
The next thing you need to make sure of is that all used classes for testing are available in test bundle and memberships are appropriately tagged. This can be done on the right-hand side menu “Target Membership” for each file. In the following example, MyModel.swift file is the one under test:
Another option is to use
@testable annotation for importing different modules.
Now, to compile tests for real device, select device to be used from the menu and press Command + B (or Product -> Build). Note that since Xcode has been changed quite a lot with the latest version, you have to deal with those tests differently based on which Xcode version you are using.
In Xcode 7 you can right click on .xctest under Product, and select Show in Finder:
In Xcode8 .xctest can be found inside of the .app, so in order to locate is right click on top of the app, Show in Finder, right click again top of the app in Finder and select Show Package Contents:
Then go to the Plugins folder and right click top of the .xctest and select compress:
Now you have XCTest package as a zip file:
These files are now usable locally with your real, physical iOS devices, or alternatively with a mobile app testing cloud service.