Unit Testing in Swift for iOS – Your Complete Guide

Learn how to implement Unit Testing.

In my previous post, we discussed what is Unit Test and why we should include it in iOS development.

In this post, I will cover how to write test classes and methods in Swift using a very basic example.

Create A Project:

 

 

If creating a new project:

  1. Open Xcode
  2. Create a “Single View Application” and name it anything you want. In my case, it will be named UnitTestExample.
  3. Click on “Include Unit Tests”

Open An Existing Project

 

If opening an existing project with no Unit Tests added:

  1. Go to “Project Settings”
  2. Make sure the main target is selected
  3. Click on “+” button at the bottom.
  4. Select “iOS Unit Testing Bundle” and click “Next”.

 

You should now have your test class files added. You will also see the test target in your target list.

Let us open our ViewController file and write a simple extension.

 

 

This extension returns a cube of an integer.

Now lets test this extension using XCTest in your test class file.

When your test class files are added using previous steps, it auto-generates a few methods including “setUp”, “tearDown”, “testCaseExample” and “PerformanceExample”. We will go through each of these methods at a later point in this post. For now, I recommend that you delete everything inside XCTest class.

Now, replace it with the following code.

 

 

Let us go through this code one line at a time.

The first step to writing a test class is to import the main project by adding @testable import <Project Name> right before declaring the class.

Secondly, within the XCTestCase, declare a function to test your extension. I prefer adding “test” as a prefix before the original name of extension or function.

Within the new function, declare a variable (in our case “val”) for the value that you want the cube for (since our extensions returns a cube of an integer).

Declare another variable (in our case “res”) for the result of the extension. In our case, “val.cube()”, where “val” is the variable declared right above and “cube()” is the extension that we wrote previously.

At this point, we are going to jump into an important concept of Test Assertion. The core of Unit Testing in iOS is you are checking a method or a case against it’s “expected value”. According to the example above, if val is 4, our expected result is 64 (cube of 4). If “res” is any other value, the test will fail. It will only pass if the “res” is 64 as long as the “val” is 4. We are testing this using a test assertion called XCTAssertEqual, which is essentially checking if “res” is 64. There are various other test assertions which we will go through at a later point in this post.

That’s all for writing your first Unit Test. For testing this case, you will need to build (cmd + b) the project and then click on the line number on the left hand side of the declared test function. This will fire the test!

Now, let’s say you want to test a function instead of an extension. Here I have written the same method as a function inside ViewController.

 

 

In order to test this, we have to follow similar steps as above. However, this time we have to add one additional item. We have to declare and initiate the ViewController within our XCTest. The test function can follow the ViewController declaration.

 

 

As you can see, variable “vc” is declaring the ViewController.

The function, CubeFunc(), within ViewController is called using “vc.CubeFunc() inside XCTest.

This was a very basic example but imagine how complex methods can be easily tested as you write your code. Imagine how you could have implemented Unit Testing in your previous projects. Imagine how Unit Testing can save you from going through those long debugging sessions.

Test Assertions

Above examples used an Equality Test Assertion called XCTAssertEqual. There are various other Test Assertions that you can use based on your methods and requirements. In order to make it easy for all of us, I have compiled the following easy-to-read list for all the available Test Assertions:

1. Equality and Inequality Test Assertions for values, optional values, arrays and dictionaries. 

  • XCTAssertEqual – Asserts two values are equal
  • XCTAssertNotEqual – Asserts two values are not equal

2. Boolean Assertions.

  • XCTAssertTrue – Asserts that an expression is true.
  • XCTAssertFalse – Asserts that an expression is false.

3. Nil and Non-nil Assertions.

  • XCTAssertNil – Asserts that an expression is nil.
  • XCTAssertNotNil – Asserts that an expression is not nil.

4. Comparable Value Assertions.

  • XCTAssertGreaterThan – Asserts that one value is greater than another.
  • XCTAssertGreaterThanOrEqual – Asserts that one value is greater than or equal to another.
  • XCTAssertLessThanOrEqual – Asserts that one value is less than or equal to another.
  • XCTAssertLessThan – Asserts that one value is less than another.

5. Error Assertions.

  • XCTAssertThrowsError – Asserts that an expression throws an error.
  • XCTAssertNoThrow – Asserts that an expression doesn’t throw an error.

6. Failing Unconditionally.

  • XCTFail – Generates a failure immediately and unconditionally.

Besides these Assertions, there are Asynchronous Tests and Expectations that you should read about. I will also cover it in one of my future posts.

In the meanwhile, try these Test Assertions listed above and let me know your thoughts.

Happy coding!

Daxes Desai

In my 10 years of iOS development experience, I have developed and helped monetize apps in various categories. So let’s talk!

Let's talk!

©2017 Apportunist.com - All Rights Reserved
@pportunist