📈 Unit Testing in Swift: Introduction

Test, test, test!
As much as it may sound like a WHO recommendation in the middle of a COVID19 pandemic, it’s not. It’s the general recommendation for any software developer who values great code quality and peace of mind while developing.
In the following series I will cover everything from why you should thoroughly test your Swift code to how to implement advanced mocking of internal classes in Swift.
The series contains the following articles:
- Introduction (you’re reading it)
- Part 1: The Fundamentals
- Part 2: Asynchronous Expectations
- Part 3: Proper Architecture
- Part 4: Mocking
- Part 5: Behavioural Assertion
With the table of contents done and dusted, let’s get on with it!
“…But why test a mobile app project?”
Every now and then I bump into developers asking me why there is a need for unit testing mobile application projects; after-all most of the code is UI-related anyway. This question is part of my motivation for writing this article, as I strongly believe that every software developer should be writing unit tests — even in solo projects. The answer to the question is simple:
“To prevent you from introducing bugs!”
As harsh as it may sound, it is the truth: Unit tests ultimately keep track of the logic within your application, so that you don’t have to remember it yourself.
In the beginning it may seem easy to remember how and why your code behaves how it does, but in time your logic will inevitably be extended, new developers may enter the arena or you may simply just forget how certain modules are supposed work. This is where unit tests save your day. With a proper test suite you will be aware of any regressions added with newly introduced code, before it’s pushed. In addition, both you and the new developer on your team will feel significantly more comfortable, knowing that no unit tests fail in the first commits.
The list of specific advantages of unit testing is long. While this should be reason enough for you to start testing, I understand that you may not be convinced just yet. So don’t take my word for it — instead listen to the community!
As always, if you have any questions or comments, feel free to reach out to me by commenting on these articles. I will reply to all messages.
Happy reading!