Unit Test View Controllers the easy way in Swift

When it come to unit test view controllers, people think of using UI Test instead of Unit Test. However UI Test is not the answer for everything related to UIKit, and today we will discuss what are the key things to tell you what and when to use each on of them.

What to unit test in view controllers

Basically, view controllers form most of our project, therefore unit test viewController is essential and will lead to better code quality.

just remember you can’t use unit test for everything related to UIKit, for instance you shouldn’t use unit test to test background colors, font-size , text color, or even Auto-layout constraints.

On the other hand we use Unit Test to test if Label text is correct, Button is enabled, IBOutlet exists, and Navigations between ViewControllers

Before we start writing tests first we should load View controllers, and that will get us into three different methods of creating them

  • Storyboard-based
  • XIB-based
  • and Code-based

and today we will discuss all of them

Now let’s learn how to do the magic.

Setup our Environment

All we need to start by writing unit test for view controllers is to create a new Xcode project and name it something like UnitTestViewControllers, and remember to enable Include Tests.

Unit test view controllers 1.0: create new Xcode project with tests included

now go to UnitTestViewControllersTests.swift and remove all the methods inside it, and hit command+U (remember to always select simulator not a physical device when testing) to run the test and make sure that everything is working, you should get same result as here

Unit test view controllers 1.1: Getting the initial test succeeded

Setup Storyboard based view controllers

Firstly Lets start by creating a view controller by going to File->New->File and chose Cocoa Touch Class and then select UIViewController as Subclass and give it the name StoryboardViewController like this

Unit test view controllers 2.0: Create ViewController for testing

Now For simplicity we will use Main.storyboard, now go and add the view controller by dragging and dropping it from library menu

Unit test view controllers 2.1: Add ViewController to StoryBoard

and give it the class of StoryboardViewController and also storyboard ID as StoryboardViewController from the inspectors area

2.2 Set ViewController class and ID

now add a UILabel to the storyboard and link it to the code

2.3 Add IBOutlet

Load Storyboard based view controllers into tests

This should be very straight forward, all you have to do now is go to UnitTestViewControllersTests.swift and write a test case for loading the storyboard view controller and then hit the command+U you should get a green mark for this

2.4 Test Loading Storyboard Based ViewControllers

now it’s time to test if the Label is loaded, we do that by adding an assertion and then run the test again

2.5 Asserting IBOutlet

as you can see the test has failed and that because our view controller not yet loaded and we should ask it to load the view first by adding sut.loadViewIfNeeded()before the assertion and that will load the view controller as well as the IBOutlet connection for the label.
Now run the test again and Voila, tests passed again.

2.6 Load view controller

Setup XIB based view controllers

Lets setup our XIB view controller , well this should be pretty much the same as storyboard one, go to File->New->File and chose Cocoa Touch Class and then select UIViewController as Subclass but this time check the Also create XIB file and give it the name XIBViewController like this:

3.0 Create XIB ViewController

new you should have two files

3.1

Testing XIB ViewController


now after that do the same step as 2.3 Add Outlet and make sure to link it to the code, now go to UnitTestViewControllersTests.swift and add a new method this time to test the XIB view controller

3.2 Test XIB view controller

and after running the test you should see the green mark again to know that all has passed.

Setup Code based view controllers

Lets setup our Code view controller, go to File->New->File and chose Cocoa Touch Class and then select UIViewController as Subclass and give it the name CodeViewController, this is different from the two types before that it doesn’t have an outlet to link, so we need to setup our UI in code, so after creating it open the file and just add the following

4.0 Setup UI in code

now in order to test this just go to the Tests file and add the final method

4.1 Test Code Based ViewController

and Hooray now we know how to load and test all the 3 types of ViewControllers.

Conclusion

we have seen today how to setup and load ViewController for testing, later we will talk about testing navigation between ViewControllers as well as how to write testable viewControllers so stay tuned.

you can find the code on GitHub.

Before you go..

if you liked this article please do clap and share it with friends.

and if you wanted to have a discussion or reach me you can go to my website and get in touch.

Leave a Reply

Your email address will not be published. Required fields are marked *