First test

Add first test and learn how to check code coverage in Xcode.

This post is a continuation of the app initialization process, it will be better if you read all of the previous posts first. Click here to see the list of previous posts.

We already have Quick and Nimble installed, we did this during CocoaPods installation. So we can start with testing right away.

The first test will be very simple. It will not test any functionality at all, in fact its only purpose is to check if testing target and the whole setup is correct.

Let’s start by creating a new TestSpec.swift file inside iOSProjectStarterSpec directory:

import Foundation
import Quick
import Nimble

class TestSpec: QuickSpec {
  override func spec() {
    describe("sample test") {
      context("first") {
        it("should pass") {

Don’t forget to select iOSProjectStarterSpec upon creation. On top import Quick and Nimble. Our test class has to derive from QuickSpec. To define test we need to override spec() function.

The describe block is here to help to create a group of examples.

The context block is very similar to describe, it will help you to define different requirements for specs.

The it block should contain a single test.

Our test is very simple and we expect it always to pass. Now start test: Product > Test (⌘U). Project should build without errors, run and after a couple of seconds you should see success:


Now we know that our test setup is working correctly. We will write proper test cases during the next project.

Code coverage in Xcode

Since Xcode 7 you can easily check code coverage for your project. It can be helpful to spot which parts of yours application are not tested enough. Code coverage for new projects is turned off by default.

To turn it on, click Edit Scheme from Product menu ( ⌘< ):

Edit scheme

Select Test action and inside Info tab, just select checkbox Gather coverage data:

Edit scheme

Now run tests (⌘U). After tests will finish running, go to Report navigator and select Coverage tab:

Code coverage

Right now there is almost no code coverage at all, but we will get back to this later in the second project when there will be some real specs.

In the next step we will take a look at Fabric and learn how to install it.