Unit Testing Your Code

iDev
4 min readFeb 9, 2021

This is a simple overview of what unit testing is, why it’s used, and different ways you can test your code.

Testing is not the most attractive or “fun” part iOS Development but it is a “necessary evil” if you will. Every good app will have some sort of testing so it's not something you want to skip over if you're getting into programming.

So what is it?

In simple terms, it’s writing code to test your code. It’s a good way that your app works the way it was designed to. By testing your app you can avoid having bugs, this really comes in handy as the app gets larger and larger.

You basically write code to test whether or not you get the right output when entering a certain input. Usually, you input data into a function and assert that the output will be a certain value. If the result is not what is expected the test will fail and you address the problem correctly.

Why is it used?

Let’s say you have made some changes to an app in the code. The app runs and builds just fine, but when using the app, the user isn't getting the expected result. There is a bug. But the app was built and ran just fine? This is because there was no error with syntax of the code or there was no unexpected missing value, but there was a problem with the logic. All values were where they were supposed to be, just not the correct values.

This could be fixed by unit testing your code. The project will build but if you had a test that was supposed to have a certain value and it did not, you would catch the bug before it was put in production.

Different types of testing

Unit testing is testing small parts or “units” of your code. Usually just within one function. This is a more narrow and precise form of testing. Allowing you to more quickly identify where the issue is.

You can also test the UI functionality or the “View Controller” in Xcode. It’s very similar to the unit testing but because you are dealing with the view controller, in the test you can basically simulate a user using the app by “tapping” buttons for example. So by tapping certain buttons, you can expect a certain result, therefore, being able to write some test code to test said result.

Testing endpoints or networking using mock data. The network requests within your app are designed to hit endpoints of a server(API) somewhere. Well, we don't want your app to actually make those requests just while we are testing the app, so what you do is write up a mock(fake) network request to simulate the app, and based on that mock request we can expect some results as well. Now we can write code tests based on the expected result.

Performance tests, making sure your app is efficient and not slowing down when changes are made. When writing a test, swift allows us to use a function called “measure,” which will run a given function ten times and return the average amount of time it takes to complete the tasks. Based on this average, you can now log this average in the test portion of the app. This will serve as a standard or control from which the app should run. Now when we change code we can see if the performance has gotten faster or slower. We can also set a deviation margin to where if it gets 5%(or less) slower, it still it passes, but if it gets slower than that, it will throw an error.

We can also test async code as well. Async code is code that is running along with other code. It is on another thread in the “background” performing tasks as well. So we can set a buffer to wait for this code to complete and then test its given result as well.

This was a quick overview of Unit Testing and why testing your code is important. I try to simplify whatever I learn to teach it to others. I hope this found you well.

Rick Martinez

--

--