Mobile Testing: A Guide to Testing Your iOS app

Project managers have always had a complicated relationship with testing. In the early days, it was considered a necessary evil that slowed the release process – but saved money in the long term. Technology has moved on since those days so, in the age of mobile development, is testing (and in particular unit testing) still the universal panacea it once was?

Relationship status: it’s complicated

It’s not just managers that have a complicated relationship with testing, developers do as well. The friction stems from a range of egotistical and psychological sources. A simple question illustrates this nicely: “What would you rather spend your time doing?”

  1. Learning how to display a table with data you fetched asynchronously from a remote server?
  2. Grinding your way through a list of tests to ensure your network stack is working as it should?

I thought so, and you’re not the only one. Testing just doesn’t have the ‘coolness’ factor – and so doesn’t appeal to the ego.

Mobile testing, which we will be focussing on in this article, has its own specific problems. I often hear from colleagues how “mobile testing is harder than testing on a server”. And there is some truth to this. Usually, newbie mobile developers assume that testing a mobile app means simulating every step a user might perform. While that’s one way of testing, it’s probably the hardest. It’s also rarely necessary. If your app is well modularized, with low coupling between different parts, you can carry out the same type of tests as a developer writing server code.

Ultimately, what it comes down to is knowing what’s worth testing and what isn’t. This article will help you answer that question. It’s a mobile testing ‘bare minimum’ guide to readying an iOS app for release. One that balances best practices with developing in the real world.

Mobile Testing 101

Let’s take a look at some key vocabulary. To start with, there are three types of testing you need to be aware of. There are multiple options here, for the same reason that there are different types of cars. If you need to move heavy equipment, you might select a pick-up. But if you have to ferry the kids to soccer practice, then a minivan is your go-to option.

Unit Testing: To test a specific piece of functionality that is isolated from everything else. This would be the case if you have to test that a calculation or procedure is being done correctly.

Integration Testing: To test multiple components at the same time. Let’s imagine an app needs to download multiple records from a web server (the <span class="token builtin">ServerService</span> module) and store them in a database (the <span class="token builtin">DataStoreService</span> module). Testing how both work together is integration testing.

Functional Testing: These are product-oriented tests that check if the product is meeting defined requirements. For example, if your app needs to show a bank statement.

Framework: XCTest is the testing framework that comes bundled and fully integrated with Xcode for iOS. It will provide us with the basic structure of a test, and also give us all the methods needed for our test assertions.
Coverage: This metric tells you how much of your codebase has been executed while running tests. It’s expressed as a percentage: 80% coverage, 20% coverage, etc. It’s a handy metric as it helps show which parts of your code have never been executed under test conditions. That being said, having 100% coverage doesn’t mean that your code is definitely bug-free. It simply means that the totality of your code has been executed while testing. There could still be a group of conditions that have not been executed in a specific order that might produce a bug. It’s a bit counter-intuitive I know.

Last on the list of essential vocab is assertions. These are expressions that encapsulate testable logic about a target under test. Simply put, it’s a condition that we will use to be sure a test failed or succeeded. For example:

<span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>someVariable<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>

Here we are making sure that someVariable is equal to 0. If someVariable was 5, then the above test would have failed.

Testing on iOS

To test an app, we’re going to need one. So I created this simple (OK, and ugly) budgeting app that will serve our purposes perfectly – but it probably won’t rank at the top of the app store! We’ll be running the app through all the above tests, including the coverage report. Essentially, we’ll be refactoring the code from a total mess to easily testable.

iOS Mobile Testing App Demo

The code driving this app screen is below. It makes a network request and shows the returned information in a UITableView where each cell is a project with its own details. Let’s take a look at the code:


<span class="token keyword">import</span> <span class="token builtin">UIKit</span>

<span class="token keyword">class</span> <span class="token class-name">ViewController</span><span class="token punctuation">:</span> <span class="token builtin">UIViewController</span> <span class="token punctuation">{</span>

    <span class="token atrule">@IBOutlet</span> <span class="token keyword">weak</span> <span class="token keyword">var</span> tableView<span class="token punctuation">:</span> <span class="token builtin">UITableView</span><span class="token operator">!</span>
    <span class="token keyword">var</span> projects<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">:</span> <span class="token builtin">Any</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

    <span class="token keyword">override</span> <span class="token keyword">func</span> <span class="token function">viewDidLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">viewDidLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        tableView<span class="token punctuation">.</span>delegate <span class="token operator">=</span> <span class="token keyword">self</span>
        tableView<span class="token punctuation">.</span>dataSource <span class="token operator">=</span> <span class="token keyword">self</span>

        <span class="token function">loadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">func</span> <span class="token function">loadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> url <span class="token operator">=</span> <span class="token function">URL</span><span class="token punctuation">(</span>string<span class="token punctuation">:</span> <span class="token string">"https://my-server.com/projects.json"</span><span class="token punctuation">)</span><span class="token operator">!</span>
        <span class="token builtin">URLSession</span><span class="token punctuation">.</span>shared<span class="token punctuation">.</span><span class="token function">dataTask</span><span class="token punctuation">(</span>with<span class="token punctuation">:</span> url<span class="token punctuation">)</span> <span class="token punctuation">{</span> data<span class="token punctuation">,</span> response<span class="token punctuation">,</span> error <span class="token keyword">in</span>
            <span class="token keyword">guard</span> <span class="token keyword">let</span> data <span class="token operator">=</span> data <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token punctuation">}</span>

            <span class="token keyword">self</span><span class="token punctuation">.</span>projects <span class="token operator">=</span> <span class="token keyword">try</span><span class="token operator">!</span> <span class="token builtin">JSONSerialization</span><span class="token punctuation">.</span><span class="token function">jsonObject</span><span class="token punctuation">(</span>
                with<span class="token punctuation">:</span> data<span class="token punctuation">,</span>
                options<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
            <span class="token punctuation">)</span> <span class="token keyword">as</span><span class="token operator">!</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">:</span> <span class="token builtin">Any</span><span class="token punctuation">]</span><span class="token punctuation">]</span>

            <span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span>main<span class="token punctuation">.</span>async <span class="token punctuation">{</span>
                <span class="token keyword">self</span><span class="token punctuation">.</span>tableView<span class="token punctuation">.</span><span class="token function">reloadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">.</span><span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">extension</span> <span class="token builtin">ViewController</span><span class="token punctuation">:</span> <span class="token builtin">UITableViewDataSource</span><span class="token punctuation">,</span> <span class="token builtin">UITableViewDelegate</span> <span class="token punctuation">{</span>

    <span class="token keyword">func</span> <span class="token function">tableView</span><span class="token punctuation">(</span><span class="token number">_</span> tableView<span class="token punctuation">:</span> <span class="token builtin">UITableView</span><span class="token punctuation">,</span> numberOfRowsInSection section<span class="token punctuation">:</span> <span class="token builtin">Int</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">Int</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> projects<span class="token punctuation">.</span><span class="token builtin">count</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">func</span> <span class="token function">tableView</span><span class="token punctuation">(</span><span class="token number">_</span> tableView<span class="token punctuation">:</span> <span class="token builtin">UITableView</span><span class="token punctuation">,</span> cellForRowAt indexPath<span class="token punctuation">:</span> <span class="token builtin">IndexPath</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">UITableViewCell</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> cell <span class="token operator">=</span> tableView<span class="token punctuation">.</span><span class="token function">dequeueReusableCell</span><span class="token punctuation">(</span>
            withIdentifier<span class="token punctuation">:</span> <span class="token string">"ExpenseDetailTableViewCell"</span><span class="token punctuation">,</span>
            <span class="token keyword">for</span><span class="token punctuation">:</span> indexPath
        <span class="token punctuation">)</span> <span class="token keyword">as</span><span class="token operator">!</span> <span class="token builtin">ExpenseDetailTableViewCell</span>

        <span class="token keyword">let</span> project <span class="token operator">=</span> projects<span class="token punctuation">[</span>indexPath<span class="token punctuation">.</span>row<span class="token punctuation">]</span>

        <span class="token keyword">let</span> budget <span class="token operator">=</span> <span class="token function">Double</span><span class="token punctuation">(</span>project<span class="token punctuation">[</span><span class="token string">"budget"</span><span class="token punctuation">]</span> <span class="token keyword">as</span><span class="token operator">!</span> <span class="token builtin">String</span><span class="token punctuation">)</span><span class="token operator">!</span>

        cell<span class="token punctuation">.</span>projectNameLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> project<span class="token punctuation">[</span><span class="token string">"name"</span><span class="token punctuation">]</span> <span class="token keyword">as</span><span class="token operator">!</span> <span class="token builtin">String</span>
        cell<span class="token punctuation">.</span>budgetLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> <span class="token function">String</span><span class="token punctuation">(</span>budget<span class="token punctuation">)</span>

        <span class="token keyword">let</span> expenses <span class="token operator">=</span> project<span class="token punctuation">[</span><span class="token string">"expenses"</span><span class="token punctuation">]</span> <span class="token keyword">as</span><span class="token operator">!</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">:</span> <span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
        <span class="token keyword">let</span> totalExpenses <span class="token operator">=</span> expenses<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">(</span>result<span class="token punctuation">,</span> nextExpense<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">Double</span> <span class="token keyword">in</span>
            <span class="token keyword">let</span> amount <span class="token operator">=</span> <span class="token function">Double</span><span class="token punctuation">(</span>nextExpense<span class="token punctuation">[</span><span class="token string">"amount"</span><span class="token punctuation">]</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>
            <span class="token keyword">return</span> result <span class="token operator">+</span> amount
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span> budget <span class="token operator">>=</span> totalExpenses <span class="token punctuation">{</span>
            cell<span class="token punctuation">.</span>budgetLabel<span class="token punctuation">.</span>textColor <span class="token operator">=</span> <span class="token builtin">UIColor</span><span class="token punctuation">.</span>green
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            cell<span class="token punctuation">.</span>budgetLabel<span class="token punctuation">.</span>textColor <span class="token operator">=</span> <span class="token builtin">UIColor</span><span class="token punctuation">.</span>red
        <span class="token punctuation">}</span>

        <span class="token keyword">let</span> dateString <span class="token operator">=</span> project<span class="token punctuation">[</span><span class="token string">"start_date"</span><span class="token punctuation">]</span> <span class="token keyword">as</span><span class="token operator">!</span> <span class="token builtin">String</span>
        <span class="token keyword">let</span> formatter <span class="token operator">=</span> <span class="token function">ISO8601DateFormatter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">let</span> date <span class="token operator">=</span> formatter<span class="token punctuation">.</span><span class="token function">date</span><span class="token punctuation">(</span>from<span class="token punctuation">:</span> dateString<span class="token punctuation">)</span><span class="token operator">!</span>

        <span class="token keyword">let</span> dateFormatter <span class="token operator">=</span> <span class="token function">DateFormatter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        dateFormatter<span class="token punctuation">.</span>dateStyle <span class="token operator">=</span> <span class="token punctuation">.</span>medium
        dateFormatter<span class="token punctuation">.</span>timeStyle <span class="token operator">=</span> <span class="token punctuation">.</span><span class="token keyword">none</span>


        cell<span class="token punctuation">.</span>startDateLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> dateFormatter<span class="token punctuation">.</span><span class="token function">string</span><span class="token punctuation">(</span>from<span class="token punctuation">:</span> date<span class="token punctuation">)</span>
        cell<span class="token punctuation">.</span>statusLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> project<span class="token punctuation">[</span><span class="token string">"status"</span><span class="token punctuation">]</span> <span class="token keyword">as</span><span class="token operator">!</span> <span class="token builtin">String</span>

        <span class="token keyword">return</span> cell
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
Swift

The data returned from the server looks like this:


<span class="token punctuation">[</span> 

  <span class="token punctuation">{</span>
<span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Fisherman Coats"</span><span class="token punctuation">,</span>
<span class="token property">"start_date"</span><span class="token operator">:</span> <span class="token string">"2018-01-01T20:01:39+00:00"</span><span class="token punctuation">,</span>
<span class="token property">"due_date"</span><span class="token operator">:</span> <span class="token string">"2018-05-17T20:01:39+00:00"</span><span class="token punctuation">,</span>
<span class="token property">"status"</span><span class="token operator">:</span> <span class="token string">"finished"</span><span class="token punctuation">,</span>
<span class="token property">"budget"</span><span class="token operator">:</span> <span class="token string">"10000.00"</span><span class="token punctuation">,</span>
<span class="token property">"expenses"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Pencils"</span><span class="token punctuation">,</span>
<span class="token property">"amount"</span><span class="token operator">:</span> <span class="token string">"100.00"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token punctuation">{</span>
<span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Fabric"</span><span class="token punctuation">,</span>
<span class="token property">"amount"</span><span class="token operator">:</span> <span class="token string">"120.00"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token punctuation">{</span>
<span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Sewing Material"</span><span class="token punctuation">,</span>
<span class="token property">"amount"</span><span class="token operator">:</span> <span class="token string">"25.00"</span>
<span class="token punctuation">}</span>
<span class="token punctuation">]</span>
  <span class="token punctuation">}</span> …
<span class="token punctuation">]</span>
JSON

The above code is bad. In fact, it’s so bad it’s untestable. Everything is tightly coupled and we are violating the single responsibility principle: each module should do just one thing and do it right. But that’s pretty much the point of this article, so don’t hold it against me!

De-cluttering the view controller

In our app, the view controller is doing the following tasks:

  • Requesting data from the server.
  • Parsing and getting a dictionary out of this data.
  • Extracting and performing different actions to the data.
  • Displaying the information.

The thing is, it should only be doing that last point. Let’s get rid of all the parsing and accessing the project data from a dictionary, we are going to do this by creating a model. A model is a representation of ‘something’: in this case a project. So we need to create a model called <span class="token builtin">Project</span> that is going to hold all the information related to the project. This project also has expenses so there we have another model called <span class="token builtin">Expense</span>.

Now we need a way of populating those models with the server data we fetched. This can be done manually, but it’s pretty clunky as you have to check if each field exists. A better option is to use Codable and ObjectMapper. So that’s what I’ll do.

I use Codable to get rid of the JSON parsing in the view controller. I won’t break this process down, as the article would stretch into an ebook. Instead, I suggest you check out Apple’s documentation. It’s OK, I’ll wait.

[ elevator music interlude ]

Great, so now our models are fully codable:


<span class="token keyword">enum</span> <span class="token builtin">Status</span><span class="token punctuation">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span> <span class="token builtin">Codable</span><span class="token punctuation">{</span>
    <span class="token keyword">case</span> finished
    <span class="token keyword">case</span> running
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">Project</span><span class="token punctuation">:</span> <span class="token builtin">Codable</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> name<span class="token punctuation">:</span> <span class="token builtin">String</span><span class="token operator">?</span>
    <span class="token keyword">var</span> status<span class="token punctuation">:</span> <span class="token builtin">Status</span><span class="token operator">?</span>
    <span class="token keyword">var</span> budget<span class="token punctuation">:</span> <span class="token builtin">String</span><span class="token operator">?</span>
    <span class="token keyword">var</span> startDate<span class="token punctuation">:</span> <span class="token builtin">Date</span><span class="token operator">?</span>
    <span class="token keyword">var</span> dueDate<span class="token punctuation">:</span> <span class="token builtin">Date</span><span class="token operator">?</span>
    <span class="token keyword">var</span> expenses<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token builtin">Expense</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">var</span> budgetValue<span class="token punctuation">:</span> <span class="token builtin">Double</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">Double</span><span class="token punctuation">(</span>budget <span class="token operator">?</span><span class="token operator">?</span> <span class="token string">"0"</span><span class="token punctuation">)</span> <span class="token operator">?</span><span class="token operator">?</span> <span class="token number">0.0</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">var</span> totalExpenses<span class="token punctuation">:</span> <span class="token builtin">Double</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> expenses<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> $<span class="token number">0</span> <span class="token operator">+</span> $<span class="token number">1</span><span class="token punctuation">.</span>amountValue <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">enum</span> <span class="token builtin">CodingKeys</span> <span class="token punctuation">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span> <span class="token builtin">CodingKey</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> name<span class="token punctuation">,</span> status<span class="token punctuation">,</span> budget<span class="token punctuation">,</span> startDate <span class="token operator">=</span> <span class="token string">"start_date"</span><span class="token punctuation">,</span> dueDate <span class="token operator">=</span> <span class="token string">"due_date"</span><span class="token punctuation">,</span> expenses <span class="token operator">=</span> <span class="token string">"expenses"</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">Expense</span><span class="token punctuation">:</span> <span class="token builtin">Codable</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> name<span class="token punctuation">:</span> <span class="token builtin">String</span><span class="token operator">?</span>
    <span class="token keyword">var</span> amount<span class="token punctuation">:</span> <span class="token builtin">String</span><span class="token operator">?</span>
    <span class="token keyword">var</span> amountValue<span class="token punctuation">:</span> <span class="token builtin">Double</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">Double</span><span class="token punctuation">(</span>amount <span class="token operator">?</span><span class="token operator">?</span> <span class="token string">"0"</span><span class="token punctuation">)</span> <span class="token operator">?</span><span class="token operator">?</span> <span class="token number">0.0</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
Swift

Each model is in its own Swift file: Project.swift and Expense.swift. Just as it should be.

How do we know if this is going to extract the values (from the JSON) and place them in the correct property? There are two possible approaches here. See if you can work out which one is optimal.

  1. Run the app, put a breakpoint after the decoding happens and start printing out each object variable.
  2. Create a short unit test to check if everything works as intended.

Options 2 it was! If you carry that out, you should see a folder called <span class="token builtin">ExpensesTests</span> in the Project Navigator window – that’s where our tests will live.

iOS Mobile Testing App Demo

We’ll add two files there, one for each model:

  • ExpensesTests.swift
  • ProjectTests.swift.

When adding the new files, you’ll choose the <span class="token builtin">Unit</span> <span class="token builtin">Test</span> <span class="token builtin">Case</span> <span class="token builtin">Class</span> and make sure you select the test target when adding the files. The tests we’ll perform on the <span class="token builtin">Expense</span> model will be in the <span class="token builtin">ExpensesTests</span> file and the ones for <span class="token builtin">Project</span> model will be in <span class="token builtin">ProjectTests</span> file. I try and follow the following rule of thumb: always name your test files with the class you want to test, followed by the word ‘Tests’.

The test files are subclasses of XCTest. All the methods written inside that class that start with the word “test” are going to be run by Xcode when testing.

Writing our first test!

First, we need to figure out how to present our test with data – and then we’ll decide what data to use. This is actually quite simple: we start by creating a JSON file, add that file to our test target, read that file and finally provide that data as the source data for our test.

We’ll use the same ‘read-file approach’ in multiple places. This means that, instead of writing a function every time,  we’ll just put that method in an XCTestCase extension. So it will always be available to our XCTestCase subclasses.


<span class="token keyword">extension</span> <span class="token builtin">XCTestCase</span> <span class="token punctuation">{</span>
    <span class="token keyword">func</span> <span class="token function">dataFrom</span><span class="token punctuation">(</span>filename<span class="token punctuation">:</span> <span class="token builtin">String</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">Data</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> path <span class="token operator">=</span> <span class="token function">Bundle</span><span class="token punctuation">(</span><span class="token keyword">for</span><span class="token punctuation">:</span> <span class="token builtin">ProjectTests</span><span class="token punctuation">.</span><span class="token keyword">self</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">path</span><span class="token punctuation">(</span>forResource<span class="token punctuation">:</span> filename<span class="token punctuation">,</span> ofType<span class="token punctuation">:</span> <span class="token string">"json"</span><span class="token punctuation">)</span><span class="token operator">!</span>
        <span class="token keyword">return</span> <span class="token function">NSData</span><span class="token punctuation">(</span>contentsOfFile<span class="token punctuation">:</span> path<span class="token punctuation">)</span><span class="token operator">!</span> <span class="token keyword">as</span> <span class="token builtin">Data</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
Swift

Now we need to create a JSON file that is a representation of the Project model. We’ll call it project.json. This is the same JSON representation that the server would return if we request the list of projects:

This is what that JSON file looks like:


<span class="token punctuation">{</span>
    <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Beach Cleaning"</span><span class="token punctuation">,</span>
    <span class="token property">"start_date"</span><span class="token operator">:</span> <span class="token string">"2018-01-01T20:01:39+00:00"</span><span class="token punctuation">,</span>
    <span class="token property">"due_date"</span><span class="token operator">:</span> <span class="token string">"2018-12-17T20:01:39+00:00"</span><span class="token punctuation">,</span>
    <span class="token property">"status"</span><span class="token operator">:</span> <span class="token string">"running"</span><span class="token punctuation">,</span>
    <span class="token property">"budget"</span><span class="token operator">:</span> <span class="token string">"3000.00"</span><span class="token punctuation">,</span>
    <span class="token property">"expenses"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
             <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Work permits"</span><span class="token punctuation">,</span>
             <span class="token property">"amount"</span><span class="token operator">:</span> <span class="token string">"200.00"</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
JSON

And finally, what we have been waiting for, a real test:


<span class="token keyword">class</span> <span class="token class-name">ProjectTests</span><span class="token punctuation">:</span> <span class="token builtin">XCTestCase</span> <span class="token punctuation">{</span>
    <span class="token keyword">func</span> <span class="token function">testProjectCreation</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token function">dataFrom</span><span class="token punctuation">(</span>filename<span class="token punctuation">:</span> <span class="token string">"project"</span><span class="token punctuation">)</span>
        <span class="token keyword">let</span> decoder <span class="token operator">=</span> <span class="token function">JSONDecoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        decoder<span class="token punctuation">.</span>dateDecodingStrategy <span class="token operator">=</span> <span class="token punctuation">.</span>iso8601
        <span class="token keyword">let</span> project <span class="token operator">=</span> <span class="token keyword">try</span><span class="token operator">!</span> decoder<span class="token punctuation">.</span><span class="token function">decode</span><span class="token punctuation">(</span><span class="token builtin">Project</span><span class="token punctuation">.</span><span class="token keyword">self</span><span class="token punctuation">,</span> from<span class="token punctuation">:</span> data<span class="token punctuation">)</span>
        <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>project<span class="token punctuation">.</span>name<span class="token punctuation">,</span> <span class="token string">"Beach Cleaning"</span><span class="token punctuation">)</span>
        <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>project<span class="token punctuation">.</span>status<span class="token punctuation">,</span> <span class="token builtin">Status</span><span class="token punctuation">.</span>running<span class="token punctuation">)</span>
        <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>project<span class="token punctuation">.</span>budget<span class="token punctuation">,</span> <span class="token string">"3000.00"</span><span class="token punctuation">)</span>
        <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>project<span class="token punctuation">.</span>budgetValue<span class="token punctuation">,</span> <span class="token number">3000.00</span><span class="token punctuation">)</span>
        <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>project<span class="token punctuation">.</span>dueDate<span class="token operator">?</span><span class="token punctuation">.</span>timeIntervalSince1970<span class="token punctuation">,</span> <span class="token number">1545076899.0</span><span class="token punctuation">)</span>
        <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>project<span class="token punctuation">.</span>startDate<span class="token operator">?</span><span class="token punctuation">.</span>timeIntervalSince1970<span class="token punctuation">,</span> <span class="token number">1514836899.0</span><span class="token punctuation">)</span>
        <span class="token function">XCTAssertNotNil</span><span class="token punctuation">(</span>project<span class="token punctuation">.</span>expenses<span class="token punctuation">)</span>
        <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>project<span class="token punctuation">.</span>totalExpenses<span class="token punctuation">,</span> <span class="token number">200.00</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
Swift

Time for a quick recap. We’ve tested that we are correctly filling a model object with the information sent to us by the server. We’ve also tested a small operation: getting the total expenses for the project.

Seems like a good time for another pop quiz! If we want to classify this test into a category, which would it be? Answer: we are testing an isolated and very specific functionality, so this is a Unit Test.

Once I have rewritten the view controller to use the models we just created, it will look like this:


<span class="token keyword">import</span> <span class="token builtin">UIKit</span>

<span class="token keyword">class</span> <span class="token class-name">ViewController</span><span class="token punctuation">:</span> <span class="token builtin">UIViewController</span> <span class="token punctuation">{</span>
    <span class="token atrule">@IBOutlet</span> <span class="token keyword">weak</span> <span class="token keyword">var</span> tableView<span class="token punctuation">:</span> <span class="token builtin">UITableView</span><span class="token operator">!</span>
    <span class="token keyword">var</span> projects<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token builtin">Project</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">override</span> <span class="token keyword">func</span> <span class="token function">viewDidLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">viewDidLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        tableView<span class="token punctuation">.</span>delegate <span class="token operator">=</span> <span class="token keyword">self</span>
        tableView<span class="token punctuation">.</span>dataSource <span class="token operator">=</span> <span class="token keyword">self</span>
        <span class="token function">loadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">func</span> <span class="token function">loadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> url <span class="token operator">=</span> <span class="token function">URL</span><span class="token punctuation">(</span>string<span class="token punctuation">:</span> <span class="token string">"http://localhost:8000/data.json"</span><span class="token punctuation">)</span><span class="token operator">!</span>
        <span class="token builtin">URLSession</span><span class="token punctuation">.</span>shared<span class="token punctuation">.</span><span class="token function">dataTask</span><span class="token punctuation">(</span>with<span class="token punctuation">:</span> url<span class="token punctuation">)</span> <span class="token punctuation">{</span> data<span class="token punctuation">,</span> response<span class="token punctuation">,</span> error <span class="token keyword">in</span>
            <span class="token keyword">let</span> decoder <span class="token operator">=</span> <span class="token function">JSONDecoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            decoder<span class="token punctuation">.</span>dateDecodingStrategy <span class="token operator">=</span> <span class="token punctuation">.</span>iso8601
            <span class="token keyword">guard</span>
                <span class="token keyword">let</span> data <span class="token operator">=</span> data<span class="token punctuation">,</span>
                <span class="token keyword">let</span> projects <span class="token operator">=</span> <span class="token keyword">try</span><span class="token operator">?</span> decoder<span class="token punctuation">.</span><span class="token function">decode</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token builtin">Project</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token keyword">self</span><span class="token punctuation">,</span> from<span class="token punctuation">:</span> data<span class="token punctuation">)</span>
            <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token punctuation">}</span>
            <span class="token keyword">self</span><span class="token punctuation">.</span>projects <span class="token operator">=</span> projects
            <span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span>main<span class="token punctuation">.</span>async <span class="token punctuation">{</span>
                <span class="token keyword">self</span><span class="token punctuation">.</span>tableView<span class="token punctuation">.</span><span class="token function">reloadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">.</span><span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">extension</span> <span class="token builtin">ViewController</span><span class="token punctuation">:</span> <span class="token builtin">UITableViewDataSource</span><span class="token punctuation">,</span> <span class="token builtin">UITableViewDelegate</span> <span class="token punctuation">{</span>
    <span class="token keyword">func</span> <span class="token function">tableView</span><span class="token punctuation">(</span><span class="token number">_</span> tableView<span class="token punctuation">:</span> <span class="token builtin">UITableView</span><span class="token punctuation">,</span> numberOfRowsInSection section<span class="token punctuation">:</span> <span class="token builtin">Int</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">Int</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> projects<span class="token punctuation">.</span><span class="token builtin">count</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">func</span> <span class="token function">tableView</span><span class="token punctuation">(</span><span class="token number">_</span> tableView<span class="token punctuation">:</span> <span class="token builtin">UITableView</span><span class="token punctuation">,</span> cellForRowAt indexPath<span class="token punctuation">:</span> <span class="token builtin">IndexPath</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">UITableViewCell</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> cell <span class="token operator">=</span> tableView<span class="token punctuation">.</span><span class="token function">dequeueReusableCell</span><span class="token punctuation">(</span>
            withIdentifier<span class="token punctuation">:</span> <span class="token string">"ExpenseDetailTableViewCell"</span><span class="token punctuation">,</span>
            <span class="token keyword">for</span><span class="token punctuation">:</span> indexPath
        <span class="token punctuation">)</span> <span class="token keyword">as</span><span class="token operator">!</span> <span class="token builtin">ExpenseDetailTableViewCell</span>
        <span class="token keyword">let</span> project <span class="token operator">=</span> projects<span class="token punctuation">[</span>indexPath<span class="token punctuation">.</span>row<span class="token punctuation">]</span>
        cell<span class="token punctuation">.</span>projectNameLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> project<span class="token punctuation">.</span>name
        cell<span class="token punctuation">.</span>budgetLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> project<span class="token punctuation">.</span>budget
        <span class="token keyword">if</span> project<span class="token punctuation">.</span>budgetValue <span class="token operator">>=</span> project<span class="token punctuation">.</span>totalExpenses <span class="token punctuation">{</span>
            cell<span class="token punctuation">.</span>budgetLabel<span class="token punctuation">.</span>textColor <span class="token operator">=</span> <span class="token builtin">UIColor</span><span class="token punctuation">.</span>green
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            cell<span class="token punctuation">.</span>budgetLabel<span class="token punctuation">.</span>textColor <span class="token operator">=</span> <span class="token builtin">UIColor</span><span class="token punctuation">.</span>red
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token keyword">let</span> startDate <span class="token operator">=</span> project<span class="token punctuation">.</span>startDate <span class="token punctuation">{</span>
            <span class="token keyword">let</span> dateFormatter <span class="token operator">=</span> <span class="token function">DateFormatter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            dateFormatter<span class="token punctuation">.</span>dateStyle <span class="token operator">=</span> <span class="token punctuation">.</span>medium
            dateFormatter<span class="token punctuation">.</span>timeStyle <span class="token operator">=</span> <span class="token punctuation">.</span><span class="token keyword">none</span>
            cell<span class="token punctuation">.</span>startDateLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> dateFormatter<span class="token punctuation">.</span><span class="token function">string</span><span class="token punctuation">(</span>from<span class="token punctuation">:</span> startDate<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        cell<span class="token punctuation">.</span>statusLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> project<span class="token punctuation">.</span>status<span class="token operator">?</span><span class="token punctuation">.</span>rawValue
        <span class="token keyword">return</span> cell
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
Swift

The main difference in the above code is how we are accessing the data. Previously, we were picking keys from a dictionary, before casting the values we assumed were correct. We were also making some calculations in the view controller. Now we have a cleaner view controller – even better – we have a small part of our app tested!

But there is still something that bothers me, something we’re not testing at all: the network call. We’re making a server call from the view controller and that’s not the right way to do it. It’s also impossible to test when done this way. One way of fixing this is to create a class that isolates this behavior. This then allows us to add tests while removing the network call from the view controller. Win-Win!

So let’s create a <span class="token builtin">ServerService</span> class which will hold all our network calls:


<span class="token keyword">class</span> <span class="token class-name">ServerService</span> <span class="token punctuation">{</span>
    <span class="token keyword">func</span> <span class="token function">fetchProjects</span><span class="token punctuation">(</span>completion<span class="token punctuation">:</span> @escaping <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token builtin">Project</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">Void</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> url <span class="token operator">=</span> <span class="token function">URL</span><span class="token punctuation">(</span>string<span class="token punctuation">:</span> <span class="token string">"http://localhost:8000/data.json"</span><span class="token punctuation">)</span><span class="token operator">!</span>
        <span class="token builtin">URLSession</span><span class="token punctuation">.</span>shared<span class="token punctuation">.</span><span class="token function">dataTask</span><span class="token punctuation">(</span>with<span class="token punctuation">:</span> url<span class="token punctuation">)</span> <span class="token punctuation">{</span> data<span class="token punctuation">,</span> response<span class="token punctuation">,</span> error <span class="token keyword">in</span>
            <span class="token keyword">let</span> decoder <span class="token operator">=</span> <span class="token function">JSONDecoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">let</span> projects <span class="token operator">=</span> <span class="token keyword">try</span><span class="token operator">?</span> decoder<span class="token punctuation">.</span><span class="token function">decode</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token builtin">Project</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token keyword">self</span><span class="token punctuation">,</span> from<span class="token punctuation">:</span> data<span class="token punctuation">)</span>
            <span class="token keyword">guard</span>
                <span class="token keyword">let</span> data <span class="token operator">=</span> data<span class="token punctuation">,</span>
                <span class="token keyword">let</span> projects <span class="token operator">=</span> <span class="token keyword">try</span><span class="token operator">?</span> decoder<span class="token punctuation">.</span><span class="token function">decode</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token builtin">Project</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token keyword">self</span><span class="token punctuation">,</span> from<span class="token punctuation">:</span> data<span class="token punctuation">)</span>
                <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token punctuation">}</span>
            <span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span>main<span class="token punctuation">.</span>async <span class="token punctuation">{</span>
                <span class="token function">completion</span><span class="token punctuation">(</span>projects<span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">.</span><span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
Swift

Now let’s test the service! This isn’t as straightforward as last time, because this time around we’re using an external network call. And as with any external dependency, we’ll need to find a way to stub it.

I’ve been trying to find the easiest way to define stubbing, and after reading almost every definition (editor: a slight exaggeration I’m sure!) on the web I opted for the one on Wikipedia: Test stubs provide canned answers to calls made during testing.

So, stubbing a network call means finding a way to make our <span class="token builtin">DataTask</span> return a fixed answer – without actually making a server call. The easiest way to do this is with OHHTTPStubs, which is extremely handy because you can integrate it using any of the most common dependency managers. If you want to learn more on dependency managers, take a look to my recent blog here.

I won’t explain in-depth how to use OHHTTPStubs (for that click here). But I will show you how to test with it!

Start by creating a <span class="token builtin">ServerServiceTests</span> in the <span class="token builtin">ExpensesTest</span> target:


<span class="token keyword">class</span> <span class="token class-name">ServerServiceTests</span><span class="token punctuation">:</span> <span class="token builtin">XCTestCase</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> serverService <span class="token operator">=</span> <span class="token function">ServerService</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">func</span> <span class="token function">testFetchProjects</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> expectation <span class="token operator">=</span> <span class="token function">XCTestExpectation</span><span class="token punctuation">(</span>description<span class="token punctuation">:</span> <span class="token string">"Fetching data from server"</span><span class="token punctuation">)</span>
        <span class="token comment">// 1.</span>
        <span class="token function">stub</span><span class="token punctuation">(</span>condition<span class="token punctuation">:</span> <span class="token function">isHost</span><span class="token punctuation">(</span><span class="token string">"localhost"</span><span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token function">isPath</span><span class="token punctuation">(</span><span class="token string">"/data.json"</span><span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token number">_</span> <span class="token keyword">in</span>
            <span class="token keyword">let</span> stubPath <span class="token operator">=</span> <span class="token function">OHPathForFile</span><span class="token punctuation">(</span><span class="token string">"full_response.json"</span><span class="token punctuation">,</span> <span class="token function">type</span><span class="token punctuation">(</span>of<span class="token punctuation">:</span> <span class="token keyword">self</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token keyword">return</span> <span class="token function">fixture</span><span class="token punctuation">(</span>
                filePath<span class="token punctuation">:</span> stubPath<span class="token operator">!</span><span class="token punctuation">,</span>
                status<span class="token punctuation">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
                headers<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"Content-Type"</span><span class="token punctuation">:</span><span class="token string">"application/json"</span><span class="token punctuation">]</span>
            <span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        serverService<span class="token punctuation">.</span>fetchProjects <span class="token punctuation">{</span> projects <span class="token keyword">in</span>
            <span class="token comment">// 2.</span>
            <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>projects<span class="token punctuation">.</span><span class="token builtin">count</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
            <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>projects<span class="token punctuation">.</span><span class="token builtin">first</span><span class="token operator">?</span><span class="token punctuation">.</span>budgetValue<span class="token punctuation">,</span> <span class="token number">10000.0</span><span class="token punctuation">)</span>
            expectation<span class="token punctuation">.</span><span class="token function">fulfill</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        <span class="token function">wait</span><span class="token punctuation">(</span><span class="token keyword">for</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>expectation<span class="token punctuation">]</span><span class="token punctuation">,</span> timeout<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">override</span> <span class="token keyword">func</span> <span class="token function">tearDown</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 3.</span>
        <span class="token builtin">OHHTTPStubs</span><span class="token punctuation">.</span><span class="token function">removeAllStubs</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
Swift

I’ll explain the above snippet step by step as there is a lot going on:

  1. We started by setting our stub with OHHTTPStubs. Then we defined a condition so all requests made to the localhost(with the path <span class="token operator">/</span>data<span class="token punctuation">.</span>json) receive the content from the file full_response<span class="token punctuation">.</span>json. This file contains the exact response the server would provide.
  2. Now for the actual test. We check the conditions to see if the response was as expected. Also, as this is an asynchronous test, we used expectations. Expectations let us test asynchronous code by defining an expectation, and then fulfilling that expectation when the test is correctly completed. At the same time, we defined a timeout, so if the timeout expires before the expectation is fulfilled, then the test will fail. The official documentation for expectations is quite straightforward, and you can take a look at it here.
  3. Lastly, we removed the stubs after the test was performed. This avoids conflicts with other tests using the same path.

Refactoring the View Controller

Our <span class="token builtin">ServerService</span>is tested and working, so the only thing left is to refactor our <span class="token builtin">ViewController</span>! We’ll start by getting rid of the content from the loadData method, and putting our tested service in its place!


<span class="token keyword">class</span> <span class="token class-name">ViewController</span><span class="token punctuation">:</span> <span class="token builtin">UIViewController</span> <span class="token punctuation">{</span>
    <span class="token atrule">@IBOutlet</span> <span class="token keyword">weak</span> <span class="token keyword">var</span> tableView<span class="token punctuation">:</span> <span class="token builtin">UITableView</span><span class="token operator">!</span>
    <span class="token keyword">var</span> projects<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token builtin">Project</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token keyword">let</span> serverService <span class="token operator">=</span> <span class="token function">ServerService</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">override</span> <span class="token keyword">func</span> <span class="token function">viewDidLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">viewDidLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        tableView<span class="token punctuation">.</span>delegate <span class="token operator">=</span> <span class="token keyword">self</span>
        tableView<span class="token punctuation">.</span>dataSource <span class="token operator">=</span> <span class="token keyword">self</span>
        <span class="token function">loadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">func</span> <span class="token function">loadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        serverService<span class="token punctuation">.</span>fetchProjects <span class="token punctuation">{</span> <span class="token punctuation">[</span><span class="token keyword">weak</span> <span class="token keyword">self</span><span class="token punctuation">]</span> projects <span class="token keyword">in</span>
            <span class="token keyword">guard</span> <span class="token keyword">let</span> <span class="token keyword">self</span> <span class="token operator">=</span> <span class="token keyword">self</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token punctuation">}</span>
            <span class="token keyword">self</span><span class="token punctuation">.</span>projects <span class="token operator">=</span> projects
            <span class="token keyword">self</span><span class="token punctuation">.</span>tableView<span class="token punctuation">.</span><span class="token function">reloadData</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">extension</span> <span class="token builtin">ViewController</span><span class="token punctuation">:</span> <span class="token builtin">UITableViewDataSource</span><span class="token punctuation">,</span> <span class="token builtin">UITableViewDelegate</span> <span class="token punctuation">{</span>
    <span class="token keyword">func</span> <span class="token function">tableView</span><span class="token punctuation">(</span><span class="token number">_</span> tableView<span class="token punctuation">:</span> <span class="token builtin">UITableView</span><span class="token punctuation">,</span> numberOfRowsInSection section<span class="token punctuation">:</span> <span class="token builtin">Int</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">Int</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> projects<span class="token punctuation">.</span><span class="token builtin">count</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">func</span> <span class="token function">tableView</span><span class="token punctuation">(</span><span class="token number">_</span> tableView<span class="token punctuation">:</span> <span class="token builtin">UITableView</span><span class="token punctuation">,</span> cellForRowAt indexPath<span class="token punctuation">:</span> <span class="token builtin">IndexPath</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">UITableViewCell</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> cell <span class="token operator">=</span> tableView<span class="token punctuation">.</span><span class="token function">dequeueReusableCell</span><span class="token punctuation">(</span>
            withIdentifier<span class="token punctuation">:</span> <span class="token string">"ExpenseDetailTableViewCell"</span><span class="token punctuation">,</span>
            <span class="token keyword">for</span><span class="token punctuation">:</span> indexPath
        <span class="token punctuation">)</span> <span class="token keyword">as</span><span class="token operator">!</span> <span class="token builtin">ExpenseDetailTableViewCell</span>
        <span class="token keyword">let</span> project <span class="token operator">=</span> projects<span class="token punctuation">[</span>indexPath<span class="token punctuation">.</span>row<span class="token punctuation">]</span>
        cell<span class="token punctuation">.</span>projectNameLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> project<span class="token punctuation">.</span>name
        cell<span class="token punctuation">.</span>budgetLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> project<span class="token punctuation">.</span>budget
        <span class="token keyword">if</span> project<span class="token punctuation">.</span>budgetValue <span class="token operator">>=</span> project<span class="token punctuation">.</span>totalExpenses <span class="token punctuation">{</span>
            cell<span class="token punctuation">.</span>budgetLabel<span class="token punctuation">.</span>textColor <span class="token operator">=</span> <span class="token builtin">UIColor</span><span class="token punctuation">.</span>green
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            cell<span class="token punctuation">.</span>budgetLabel<span class="token punctuation">.</span>textColor <span class="token operator">=</span> <span class="token builtin">UIColor</span><span class="token punctuation">.</span>red
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token keyword">let</span> startDate <span class="token operator">=</span> project<span class="token punctuation">.</span>startDate <span class="token punctuation">{</span>
            <span class="token keyword">let</span> dateFormatter <span class="token operator">=</span> <span class="token function">DateFormatter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            dateFormatter<span class="token punctuation">.</span>dateStyle <span class="token operator">=</span> <span class="token punctuation">.</span>medium
            dateFormatter<span class="token punctuation">.</span>timeStyle <span class="token operator">=</span> <span class="token punctuation">.</span><span class="token keyword">none</span>
            cell<span class="token punctuation">.</span>startDateLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> dateFormatter<span class="token punctuation">.</span><span class="token function">string</span><span class="token punctuation">(</span>from<span class="token punctuation">:</span> startDate<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
        cell<span class="token punctuation">.</span>statusLabel<span class="token punctuation">.</span>text <span class="token operator">=</span> project<span class="token punctuation">.</span>status<span class="token operator">?</span><span class="token punctuation">.</span>rawValue
        <span class="token keyword">return</span> cell
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
Swift

Where does that leave us? The <span class="token builtin">ViewController</span> is only doing view-related stuff – which is how it should be. It’s not making server calls, it’s not parsing JSON, and it’s not doing calculations. All our models and server calls are also tested and working as expected. When we run a test, now we should see something like this:

iOS Mobile Testing App Demo

But how do we know if anything still needs to be tested? This is where coverage comes into play.

To be able to see code coverage statistics, we’ll need to enable it. Go to  Product › Scheme › Edit Scheme and check the ‘Gather coverage for all targets’ option:

iOS Mobile Testing App Demo

Once that setting is in place, every time a test is run, we’ll see a new ‘Coverage’ category in the report navigator.

iOS Mobile Testing App Demo

In our example, <span class="token builtin">ServerService</span> is fully tested, but <span class="token builtin">Expense</span><span class="token punctuation">.</span>swift and <span class="token builtin">Project</span><span class="token punctuation">.</span>swift are not. How is this possible considering we tested all the methods in each class? Expand the <span class="token builtin">Expense</span><span class="token punctuation">.</span>swift and <span class="token builtin">Project</span><span class="token punctuation">.</span>swift line items and you’ll see the more detailed coverage information.

iOS Mobile Testing App Demo

Here we can see that some edge cases were skipped. Double click on <span class="token builtin">Project</span><span class="token punctuation">.</span>swift and Xcode will take you directly to that class.

iOS Mobile Testing App Demo

Here you can see what still needs to be tested. Let’s walk through this process:

  1. The number showing on the right side indicates how many times that line has been executed while running the tests. Here we can see that the budgetValue ran twice.
  2. The red and white dashed line in the right margin shows which lines were not tested.
  3. The red text shows the specific code that did not run in the tests.

Let’s remedy this by adding the missing text to provide a budget variable that is ‘nil’ and a budget variable that is a ‘non-valid’ number.


<span class="token keyword">func</span> <span class="token function">testProjectEmptyBudget</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token function">dataFrom</span><span class="token punctuation">(</span>filename<span class="token punctuation">:</span> <span class="token string">"project"</span><span class="token punctuation">)</span>
    <span class="token keyword">let</span> decoder <span class="token operator">=</span> <span class="token function">JSONDecoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    decoder<span class="token punctuation">.</span>dateDecodingStrategy <span class="token operator">=</span> <span class="token punctuation">.</span>iso8601
    <span class="token keyword">let</span> project <span class="token operator">=</span> <span class="token keyword">try</span><span class="token operator">!</span> decoder<span class="token punctuation">.</span><span class="token function">decode</span><span class="token punctuation">(</span><span class="token builtin">Project</span><span class="token punctuation">.</span><span class="token keyword">self</span><span class="token punctuation">,</span> from<span class="token punctuation">:</span> data<span class="token punctuation">)</span>
    project<span class="token punctuation">.</span>budget <span class="token operator">=</span> <span class="token constant">nil</span>
    <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>project<span class="token punctuation">.</span>budgetValue<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">testProjectWrongBudget</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token function">dataFrom</span><span class="token punctuation">(</span>filename<span class="token punctuation">:</span> <span class="token string">"project"</span><span class="token punctuation">)</span>
    <span class="token keyword">let</span> decoder <span class="token operator">=</span> <span class="token function">JSONDecoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    decoder<span class="token punctuation">.</span>dateDecodingStrategy <span class="token operator">=</span> <span class="token punctuation">.</span>iso8601
    <span class="token keyword">let</span> project <span class="token operator">=</span> <span class="token keyword">try</span><span class="token operator">!</span> decoder<span class="token punctuation">.</span><span class="token function">decode</span><span class="token punctuation">(</span><span class="token builtin">Project</span><span class="token punctuation">.</span><span class="token keyword">self</span><span class="token punctuation">,</span> from<span class="token punctuation">:</span> data<span class="token punctuation">)</span>
    project<span class="token punctuation">.</span>budget <span class="token operator">=</span> <span class="token string">"this is unexpected value"</span>
    <span class="token function">XCTAssertEqual</span><span class="token punctuation">(</span>project<span class="token punctuation">.</span>budgetValue<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
Swift

I also removed all the unused methods from the <span class="token builtin">AppDelegate</span>, and added missing tests to <span class="token builtin">Expense</span><span class="token punctuation">.</span>swift. So now the coverage report looks like this:

iOS Mobile Testing App Demo

When do you stop mobile testing?

We currently have 68% coverage. Is that good enough? Well, that depends. If you are writing code for a flight navigation system where your code is responsible for keeping an airplane in the air, I would say no, 68% is not enough. I certainly wouldn’t board that plane. This question of ‘When is enough, enough?” is the million dollar question in testing. There is no bright line here, which I why I always answer with “it depends.”

Most of the time, testing the core parts of your application is enough. By ‘core parts’, I mean all the fundamental pieces of code that are going to be used again and again by your app. That’s because you should always build on top of solid code. In our example, we tested all of the core parts of the application. The only thing that is not completely tested is the ViewController. Does it make sense to test the ViewController? I would say no, I don’t want to waste energy on something that is probably going to change quite often (new designs, a different way of displaying the information, etc.).

In the end, it’s always going to come down to a judgment call. And the more you develop and test the better you will get at making that call.

Conclusion

We covered a lot in this mobile testing article. So I think a quick recap is in order:

  • We went from a completely untestable project, with all the app logic in the ViewController, to a modular and easily testable app.
  • We looked at the cost/value aspects of testing to avoid over-zealous testing approaches.
  • Finally, we explained why you should write code with testing in mind, as this will help you write more modular code – even if you end up not testing it!

Mobile development is a relatively immature field (compared to web app development anyway). I see this as an opportunity to change perceptions. We can write best-practice documentation that shows the true value of integrating mobile testing into the development process without the OCD baggage that is normally associated with it!

Are you looking to hire Mobile Developers? Every Scalable Path developer has been carefully handpicked by our technical recruitment team. Contact us and we’ll have your team up and running in no time.

Editor’s note: This article was originally published on October 10, 2018, and was updated on March 13, 2020.