A Complete guide to Unit Testing in Swift- Part I

Unit Testing basics in iOS

Nishan
4 min readJun 28, 2019
Photo by Émile Perron on Unsplash

Part II: https://medium.com/@nishan/a-complete-guide-to-unit-testing-in-swift-part-ii-d714b43da07c
Part III: https://medium.com/@nishan/a-complete-guide-to-unit-testing-in-swift-part-iii-cdebcb5d1904

Unit Testing is one of the method in Software development which ensures that the code you wrote works & will continue to work as intended in future. XCTest is the default Unit Testing framework available for iOS.

In this series we are going to cover basics of Unit Testing, about writing testable code, mocking your code, writing tests in a real world scenario for both synchronous & asynchronous methods which uses delegates & closures as callback.

Why Unit Testing?

Alice is the best developer in her organization. She is tasked with creating a beautiful app. She wrote complex code with few to none bugs, app is shipped and it works perfectly. Everyone is happy.

After few months, Bob is hired in the same organization. His task is to refactor some code and add new features. During this process, he breaks Alice’s code. This crashes the app resulting in mayhem.

Although this is a hypothetical scenario, this is what Unit Testing would have exactly prevented. Unit Testing helps in catching the bugs & errors during development, preventing this type of scenario later in production.

Integrating Unit Testing in iOS

Using Xcode, we can leverage the default XCTest framework for unit testing our code.

Whenever you create a new project, check on the Include Unit Tests option to include Unit Testing framework.

If you have existing project, You can add integrate Unit Testing by tapping + on your project editor and adding a new Unit Testing Bundle.

Adding Unit Tests target

In your project, you will see a group or folder with a name ending with Tests. In above project it would be Unit-TestingTests. This is where we create the files related to tests and run it. Open that folder, you can see that a file has already been created for you. I’ll explain it in brief.

  1. The line at the top “@testable import [projectname]” enables us to access any class or code that we write in our project. You should add this line to every test file that you create.
  2. Whenever you write a method to test your usecase, it should always start with “test” prefix. For example, testOrderPaymentWithCoupon() or testOrderPaymentWithNoBalance() etc.
  3. setup() & teardown() are executed for each of your test methods. setup() is called before each test method executes & teardown() is called after each test method executes.

Implementing Unit Testing

Suppose you are tasked with creating a basic calculator. Here is the buggy code that you wrote to meet the deadline.

Calculator.swift

But since you are a good developer, you wrote the tests.

CalculatorTests.swift

Here i have written test cases for addition, subtraction, division and multiplication. For each test case, you can create an instance of Calculator class and provide an input. Lastly you check if the output provided by Calculator class matches with your required output.

“XCTAssert()” is one of the method available in XCTest framework to ease Unit Testing. There are lots of methods available to test boolean expression, nil expression, asynchronous operation etc.

Note: All the test methods in CalculatorTests.swift file starts with “test” prefix.

You can run all the tests present in your project with Cmd + U or Just tap on 🔸icon present beside your Test class name. In this case you will see that the test fails for Subtraction & Division case. This is how Unit Testing prevents you from shipping your buggy code.

Just fix the subtract & divide method in your Calculator class and run the test again. You will see all the tests passes with a green check mark next to it.

This part covers the basics related to Unit Testing in iOS. We will cover real world scenario where we we test our business logic, learn about creating mocks in next part.

Part II: https://medium.com/@nishan/a-complete-guide-to-unit-testing-in-swift-part-ii-d714b43da07c

--

--