Unit Testing in Swift — XCode

Cem Eke
5 min readFeb 18, 2022

--

Today we will discuss about the Unit Testing in XCode.

Unit testing is an important part of our applications. Unfortunately; we skip this part to save time and achieve the project deadline. So what is that?

Unit testing helps us make sure that our app logic is running correct and stable. This is really important because we have to know our code is correct. None of us wants to go back and check the codes again. Sometimes, we miss the project deadline or objectives due to lack of Unit Testing check point.

We perform Unit Testing via Apple’s XCTest framework.

How to add “Unit Testing Target”

Let’s add unit testing target to our app so that we can verify all logic. First, we need to click on the Test Navigator Tab.

Left side in the XCode.

Then, we should add test target via “+” button.

In the Left-Down corner.

Ok! Now we will see the new XTests.swift class. XCTest is subclasses of XCTestCase so our class inherits the XCTestCase. This class includes some functions which are ready to use. I want to explain setUpWithError & tearDownWithError.

setUp() method is used to customize the initial state before the tests run. For example, initializing a data structure in the test methods and resetting its initial state for every test case.

tearDown() method is used to clean up after the tests run. For example, to remove any references we set the instance of initialized data structure to nil.

Usually we add ‘test’ keyword before new Unit testing functions like testFunctionX. In this way, we can easily understand which function is testing. Each test functions has diamond shape in the gutter on the line of the function declaration. When you click this diamond you can test your spesific function. This diamonds has already in the test navigator.

Let’s create some basic functions to write unite test to these functions. I create three functions.

The first function is that calculate biggest number in the given array. Second one is doing same thing but it will give us smallest number and the last one will create a new array which includes the numbers bigger than X.

Writing Unit Testing for the functions

We can go back to XTests class and create the first unit testing method for the biggestNumber function. But I would like to remind one thing as i say before we create a unit test methods with ‘test’ keyword.

Let’s create a testBiggestNumber method. At the same time i need to initialize our class but i won’t do that in this method because we will use that many times.

Let’s deep in to our testBiggestNumber method!

We have some important default XCTAssert functions, and we will get help to them. We will review a few of them but ofc. you can search for details. Some of the most used are: XCTAssertTrue, XCTAssertFalse, XCTAssertNotEqual, XCTAssertEqual, XCTAssertNil and XCTAssertNotNil.

Now we need to function result so i can compare to correct result. Before that we need to make sure biggestNumber method does not Nil. So we use Try-throws for that and assigned function result to biggestNumber.

Finally, we can use XCTAssert functions here. First, i will start with XCTAssertEqual. This method compares two parameters whether they are equal or not. So, the first is a biggestNumber which is our main method. Second parameter is that “5” which is we know the correct number of the result.

We do the same examples with other functions but this time i use different XCTAssert functions like NotEqual & NotNil.

Run Unit Testing

When you click the diamond on the left side, you can test your specific function. If your test target is running correct so green tick pops up. If not we can see what’s wrong. But i want to test all functions so i click to XTest’s diamond.

XCode gives us some explanations when we get errors. To see an example of error message, a wrong parameter is given in testSmallestNumber method above. So that we can simply see how an error message looks like. Ok we can fix it now!

Test Plans

Test plans allow us to running tests more than one test with different settings and define all testing variants on the same point of view. It supported in Xcode and xcodebuild for CI/XCode Server.

Assume that we have a localization fields and our functions include some string parameters for example “Apple” in English. Consider that the user language is German. What happens? This is just simple example. We may need different test settings. We use test plans for this.

Also you can always run unit test targets with different test plans.

Summary!

In conclusion, Unit testing is only required to test each function independently. This testing saves time and cost but lots of companies still do not realize this. Before ending this article, I’d like to end this article with a summary of Unit Testings advantages and disadvantages below.

Advantages and disadvantages of unit testing

Advantages to unit testing include:

  • The earlier a problem is identified, the fewer compound errors occur.
  • Costs of fixing a problem early can quickly outweigh the cost of fixing it later.
  • Debugging processes are made easier.
  • Developers can quickly make changes to the code base.
  • Developers can also re-use code, migrating it to new projects.

Disadvantages include:

  • Tests will not uncover every bug.
  • Unit tests only test sets of data and its functionality — it will not catch errors in integration.
  • More lines of test code may need to be written to test one line of code — creating a potential time investment.

For more information on unit testing in Xcode, I can recommend watching this talk from WWDC 2019!

See you on another topic…!

--

--