Setting Up KIF Integration Tests for iOS
Square released a pretty neat Integration Testing library called KIF last year and we have been using it to test the challenges for our upcoming Try iOS course on Code School. The instructions in the KIF README on setting up an Xcode 4 project are missing a couple of crucial steps, and after much Googling and Stackoverflowing I finally figured out how to get it to work, so I figured I’d share the process we are using for setting up KIF projects.
I’m assuming you already have an Xcode 4 (version 4.5.1) project that you want to add KIF to. First, if you aren’t already working with an Xcode workspace, you will need to create one for this project. To do so, choose the “Save as Workspace” menu item:
You need a workspace so you can manage both your original project (in my example InstagramClone), and the KIF project we are about to add the the workspace. For more on workspaces check out this Apple doc.
Next, you’ll want to download the KIF project from their downloads page, and copy the files inside of a “Frameworks” directory under the Project Root.
Next, go back to Xcode and command-click on empty area in the Project Navigator (⌘1), and choose “Add Files to Project”:
Navigate and select the “KIF.xcodeproj” file under Project Root->Frameworks->KIF. Now the Project Navigator should look like you have two projects on equal footing:
This is important, at this point it should not look like the “KIF” project is under the “InstagramClone” project.
Next, select your project in the Project Navigator and you should see information about Projects and Targetings in the main content area. Command-click on your app target and choose “Duplicate”:
Then choose “Duplicate Only”:
You should see a new target named “PROJECTNAME Copy”. Select the target, press Enter, and change the name to something like “Integration Tests”:
With the “Integration Tests” target selected, go to the “Build Phases” panel, expand the “Link Binary With Libraries” section, and click the little “+” button. Select the “libKif.a” binary from the list and add it:
Still with the “Integration Tests” target selected, go to the “Build Settings” panel. Make sure you have “All” and “Combined” selected:
Search for the “Header Search Paths” and edit the setting to add
$(SRCROOT)/Frameworks/KIF, like so:
Search for the “Other Linker Flags” setting and edit to add these flags:
Next, add the
RUN_KIF_TESTS=1 Preprocessor Macro to both the “Release” and “Debug” targets, like so:
Now that we’ve configured the “Integration Tests” target correctly, we need to make sure that before Xcode builds the target, it also builds the KIF project. To do this we need to do a couple of things (this is what you won’t find in the instructions provided by Square).
First, we need to edit the “PROJECTNAME Copy” Xcode Scheme to tell it to build KIF. To do that, go to “Edit Schemes” and select the “PROJECTNAME Copy” scheme. Choose the “Build” phase. You should see something like this:
Click on the little “+” button right above the “Manage Schemes” button, and Add the “KIF” target:
Close the “Edit Schemes” dialog and go back to the Project Navigator. Click and Drag the “KIF” project to under your App project. In the dialog that appears, select both your app target and the “Integration” Tests target in the “Add to targets” section:
After clicking “Finish” your Project Navigator should look like this:
Now you should be able to follow the rest of Square’s instructions for adding the Controller, Scenario, and Step files to test your app in the “Example” section of their README