How To Write Unit Test For Any iOS Project?

Cem Kazım
4 min readJan 13, 2023

You know that Unit Test has now become a culture for all software areas. In this article, we will see both the unit test definition and how it can be created and used in an iOS project.

What is Unit Test?

Unit-testing lets you specify behavior that your code must exhibit to ensure that its functionality remains unchanged as you modify it to, for example, make performance improvements or fix bugs. A unit of code is the smallest testable component of your code — for example, a method in a class or a set of methods that accomplish an essential purpose. A test case exercises a unit of code in a specific way; if the result of the test is different from the expected result, the test case fails. A test suite is made up of a set of test cases. You can develop one or more test suites to test different aspects of your code.

If you’re ready, let’s start.

Run 🏁

First of all, let’s create a project.

Let’s select on the Test Navigator tab and click on the plus button at the bottom left. Then let’s add a new unit test target by clicking on the “New Unit Test Target…” option.

Test Navigator tab

I love this project name :D, anyway. Let’s continue by clicking the Finish.

After these steps, a class like this will be created in our project.

Here we see two important methods; setUpWithError and tearDownWithError.

If we read the comments inside these methods, we will see the definitions of these methods. But for more detailed information:

setUpWithError: Provides an opportunity to reset state and to throw errors before calling each test method in a test case.
tearDown: Provides an opportunity to perform cleanup after each test method in a test case ends.

Let’s start with an example like the one above. I created a view model named BorderlineViewModel. In this class I created an array called “ageList” with my previous age and current age. And I wrote a method that filters my current age from this list.

Apparently, it seems to be working correctly. Then I can commit and push. But what if these data changes tomorrow or the next day? We will write unit test class for this. If there is a change, this class will notify us.

System under test (SUT) refers to a system that is being tested for correct operation. We set mock values for this property.

testListAreNotNilWhenIFilterMyNextAge: This method filters the “ageList” based on the value 27 and tests whether the list is empty.
testTheSetAgeIsEqualToTheFilteredAge: This method tests whether the entered static value of 27 is equal to the first value of “ageList” in the view model.

Click on the project name above and go to the “edit scheme” section.

Let’s activate the “code coverage” option, select “some targets” and add “UnittunMuBeniTests” target to the list.

When the test button on the top is pressed, all tests will be successful.

Coverage results

You can produce your test cases by writing similar methods.

Thank you for reading.

--

--