Heading image for post: Setting up Kiwi as your iOS unit testing framework


Setting up Kiwi as your iOS unit testing framework

Profile picture of Gabriel Reis Profile picture of Thais Camilo

It is well known that we at Hashrocket are advocates for the testing culture. After years of working with Ruby and Rails we've tried all kinds of testing frameworks, and there's no question that we like RSpec.

We feel that testing iOS apps should be similar, and we've being trying out different testing frameworks. Among them is Kiwi which has a RSpec-like syntax for Objective-C.

It was a nightmare to setup Kiwi 1.0 with Xcode 4. However, everything is much easier with Kiwi 2.0 and Xcode 5.

You will find extensive documentation about the subject at Kiwi's wiki, but we would like to show you a simpler approach.

Creating the project

Create a new empty Xcode 5 project. For testing propose our app will be called "KiwiExample".

Xcode 5 will create a target called "KiwiExampleTests" for unit testing using framework XCTest. It will also generate a test case file containing 1 test example.

To run the test go to "Product" -> "Test" on Xcode menu or use the shortcut "CMD+u".

If everything is correct you will get a "No implementation" failure for your test.

Failure Now that you know that XCTest is working, go ahead and remove this file because we won't use it any longer.


To easily manage dependencies we will use CocoaPods. Installation is simple, on terminal:

gem install cocoapods

Inside the project's directory, generate the pod file:

pod init


Edit the generated file 'Podfile' to add Kiwi dependency:

target "KiwiExample" do

target "KiwiExampleTests" do
  pod 'Kiwi/XCTest'

Install dependencies:

pod install

Cocoapods will generate a workspace called 'KiwiExample.xcworkspace'. Make sure to close your Xcode project and open the workspace instead.

Now you can start writing specs.

Writing tests

To demonstrate how simple Kiwi syntax is, we will be writing a spec to make sure the sum (+) method is working properly.

To create a new test file go to File -> New -> File (or CMD-n). Select the option "Objective-C test case class" from the iOS - Cocoa Touch menu and click Next.

New File Provide a name for the new file. For our app example we will call it "MathSpec".

Select the subclass type, we will be using "XCTestCase", click next and choose were the file will be stored. Make sure the checkbox option "KiwiExampleTest" target is checked and click to create the file.

Create new file Xcode will open the generated file automatically.

This file contains a default template based on the XCTestCase. Since we are not using any of this, you can remove all the content and use the following code:

#import "Kiwi.h"


describe(@"sum", ^{
  context(@"with 2 numbers", ^{
    it(@"returns the sum of the two numbers", ^{
      [[@(40 + 2) should] equal:@42];


Now use CMD+u to run your spec and Xcode will display the results for your test.

Test passed

As you can see, the DSL to write specs is pretty much the same as in RSpec. You can call the methods "describe", "context", "it" with a description and a block as the arguments. You can also do the assertions with any object with the method "should*". Mocks and stubs are also available. To more information, go to the Kiwi's wiki.

More posts about iOS testing Mobile