Understanding the importance of testing
For many developers, testing is an unnecessary overhead they must deal with when writing code.
This way of thinking is somehow understandable. We’ve finished writing our code, built an application, and seen that everything runs as expected. Instead of moving to our next task, we need to change the target, adding a test function just so we can see again that it works fine. Why waste our time on it?
Also, in many cases, writing these test functions takes a lot of work. How can we test a SwiftUI view or a network call? What does it even mean?
These all summarize why testing is not a common practice, or at least not enough.
The root of this problem is how developers approach testing and writing code in general. Testing is more than checking whether our functions run as expected; it’s about code structure, separation of concerns, the writing process, working culture, and how we treat our day-to-day jobs.
Let’s look at the following function:
func canUserAddTask(to list: List, user: User) -> Bool { if list.isLocked { return false } if !list.allowedRoles.contains(user.role) { return false } return [.privateList, .publicList].contains(list.sharingAttribute) }
This function checks whether a user can add a task to a specific list based on criteria, such as permissions, list type, and status. Now, imagine we need to ensure that this function works properly. How can we do that? Do we need to run our app in different states to see the results?
We all know that ensuring our code runs correctly is part of our development process. This is a classic example of how writing test cases and running an app in different states can ease our development process. We understand why testing is so important when adding future tasks such as refactoring and bug fixes.
Before we delve into Swift Testing, let’s understand the testing history in Apple platforms.