01 logo

Introduction to Unit Testing in iOS – Part 1

Unit testing - A developer's tool

By ConficlePublished 2 years ago 5 min read
1

Hello all, we are back with our next article. In case you would like to refer to our previous articles, you can find them here.

In our last article we discussed about MVC architecture, its problem and pros and cons.

In our future articles we will discuss about modern mobile development architectures and how these architectures resolve the problems with MVC architecture and focus on application testability, scalability and flexibility.

However before starting to discuss about these architectures we will talk about unit testing as it is at the core of modern mobile development architectures and unit testing is one of the reason behind invention of these architectures.

So let us start on it. Just to highlight all the examples and code snippets below will be mostly in swift language.

What is Unit Testing?

I am sure most of developers reading this article will be aware of this term. However for sake of beginners let us define this.

Unit tests are typically automated tests written and run by software developers to ensure that a section of an application (known as the "unit") meets its design and behaves as intended.

For example let us consider a very simple example of method that accepts 2 integers and returns their summation.

func add(number1: Int, number2: Int) -> Int {

return number1 + number2

}

The behaviour expected by above method is to provide sum of 2 number provided as an input to above method.

So for above we will have at least one test case for testing the sum of 2 numbers. We can add other edge cases like by passing both arguments as zero and then check the sum.

Each expectation is a single test case.

Scope of Unit Testing

Our application source code can be mainly divided into 2 categories.

  1. UI or User Interface code: This is code which is used to create views and screens visible to user and this code makes use UI frameworks like UIKit, SwiftUI etc.
  2. Non UI code. All the code other than UI code is considered as non UI code. For example business logics, validations, data management, helpers, utilities etc

Unit testing targets all the non UI code which contains different types of logic codes like

  1. Business logic
  2. Presentation logic
  3. Parsing logic
  4. Validation logic

Generally unit tests are written for all the public methods of any given class. Each behaviour of each method comprises of single unit test case. We will try to see this with example later in this article.

How to add Unit Test in project

We can add unit tests to our project while creating a new a project as shown in below picture

Or we can add unit test to our existing project by adding the new target by navigating File > New > Target > Test as below

After following any one of the method above unit tests will be added to our project. After adding unit test you will see

A separate folder containing a sample test class in project.

And a unit test target will be added to our application target

Example

Let us now see by an example. How can we write unit tests. Consider we have a class LoginValidator which validates email and password entered by user. Class looks as below.

The above class has 2 public methods isValid(emai: String) and isValid(password: String) for validating email and password respectively. Both these methods returns true for valid email or password and returns false value for invalid email or password.

Let us know create a unit test class for LoginValidator class. By convention unit test classes are named as classname+tests. So for LoginValidator class name of unit test class will LoginValidatorTests. Also all the test methods in unit test class have prefix test. We will see in code snippets below.

So right click on unit test targe and select “New File..”, then select “Unit test case class” and enter name of class as shown in below images.

Make sure unit test class is part of unit test target and not any other target

After following above steps we will have new unit test class as below

Note the @testable import statement. This is required to access the application classes which are required to write the unit tests.

Let’s add some unit test for our public method now. Below is how our unit test class looks like after adding the test cases for both the methods

So as you can see in above code snippets we have added below required test cases for validating both email and password.

For Email

  1. Blank email test case
  2. Valid email test case

For Password

  1. Blank password test case
  2. Password less then required length test case
  3. Valid password test case

To execute unit test for entire class press the square symbol in front of class declaration and to execute unit test for single method press the square symbol in front of that method.

To execute the complete test suite for project use command+u.

With this we have came to an end of this article. Hope you have now basic understanding what is unit testing and how can we write unit tests in iOS. We will come back in Part 2 of this article with some advanced concepts of unit testing used in modern mobile development. Till then try yourself to write some unit tests.

Thanks for reading this article. If you have any queries related to this topic or iOS, Objective C and Swift. Please write us at [email protected] or direct message us on instagram at conficle(instagram username).

Also keep watching this space for upcoming articles on iOS development, software development and technology concepts.

tech news
1

About the Creator

Conficle

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.