Anyone who has an app developed wants it to be a success. The first thing you notice as a user is the app's design and how easy it is to use. Everything has to look spot on and work intuitively, or users quickly drop off.
You may know the feeling. You are scrolling through Instagram, the app suddenly closes itself and you no longer know where you were. App crashes and other annoying bugs are killers for the user experience. All these annoying scenarios can be avoided by taking them into account during development. Test-driven development leads to a better result in the long term. Have we caught your interest? Then read on.
In this blog we go deeper into what test-driven development is, how it is applied in practice and what its biggest advantages are.
This blog post was written by our iOS developer Mark Meesters.
What is test-driven development?
Test-driven development (TDD) is a development method that differs from 'normal' development, because you work the other way round. Normally you first develop all the functionality (screens, interactions, actions and so on) and then write tests to validate that all the code you wrote works as it should. If tests fail, you adjust the code afterwards.
With test-driven development, you approach this differently.
1. You start by analysing all the requirements for the app.
2. Next, you set acceptance criteria that must be met to deliver the app successfully.
3. The next step is writing tests based on those acceptance criteria.
I can hear you thinking: how do you write tests when you have not written a line of code yet? That is exactly what makes test-driven development unique. You write the test case first, and once you have it, you make sure the test can actually pass.
A practical example of test-driven development
Imagine a developer wants to build an app for keeping track of household chores. One of the acceptance criteria set in advance is that it must be possible to create new tasks. In that case, a test scenario is written in which we create a household task. In this test, a household task is created and then saved. If we see a new household task after running the test, the test has passed. At first the test will keep failing because no code has been written yet, but with this method you keep writing code until the test scenario based on your acceptance criteria passes.
We keep improving the code without changing its behaviour, because that behaviour is defined by the acceptance criteria.
What are the advantages of test-driven development?
- Bugs are found in time
Let's start with the most important one: bugs are found early and almost never make it into a release of the app. Because we start by writing the tests and only then the code, everything is always tested before a release.
- Releases are reliable and secure
This ties in with finding bugs in time. If we develop the application test-driven, the releases that go to the client are reliable and secure. All code has been tested first, and every test has passed before a new release of the app is made. That gives not only the developer a good, safe feeling, but the client too, who knows there are practically no errors left in the code and the app works exactly as it should.
- Saves costs on manual testing
As mentioned earlier, using test-driven development means there are tests for every piece of code you write. So you no longer have to test, or have someone test, a lot of functionality by hand, which saves you time and money with every release. Of course there are always edge cases you still want to test yourself, such as the app's interaction (UX), but you can then give those your full attention, instead of focusing every time on basic features such as logging in or creating a household task.
- A bug never happens twice
A bug? Weren't those impossible with test-driven development? Unfortunately, even with TDD a bug can turn up in your app. However well you try to test everything, users can be so unpredictable that a bug still slips through. But if that happens, the TDD approach makes sure the bug never comes back. A new test is written for every bug. And now it is an open goal: because everything is tested before every new release, this bug is automatically tested too, so there can never be a release in which this test fails again.
Releases are reliable and secure
- Clean code
As a developer, you always aim for clean code. Because you write tests first and only then code, every piece of code is written specifically for that test. That way, every piece of code has a single responsibility. This makes your code very easy to understand and keeps development clear. That is good not only for the developers, but also for the client, because errors in the code can be isolated and fixed quickly.
What are the disadvantages of test-driven development?
Wait, what, there are disadvantages? I can see your attention slowly drifting, but as with any method, it is wise to name the downsides. The biggest disadvantage for someone who wants an app developed is that the project takes longer, development costs are higher and the very first release will most likely be later than originally planned. That is because with this method you do not start developing the app straight away: you first write tests and only then your code. As a result, you will see results less quickly at the start of the project, which can cause tension.
But is a fast result always a better result? In practice, it almost never is. If many bugs are found after the release, that can seriously delay the project.
- Development with this method will take more time (= money), but in the end you will save countless hours of endlessly retesting the basic features.
- Bugs that would otherwise be overlooked will not occur, so you will not disappoint your users with unnecessary crashes or bugs.
- Stakeholders who used to be nervous before an app release will now be eager to release the new app with new features to their customers.
- We could go on for a while, but I think it is clear by now.
Test-driven development in practice

Whenever we talk about TDD, the metaphor of a traffic light often comes up. It fits this method perfectly.
As everyone knows, a traffic light has three colours: red, amber and green. Each colour has a clear meaning in traffic:
- Green: Go! You can drive on, you have right of way.
- Amber: Do I brake or speed up a little? That is the question everyone asks themselves. But according to the rules, you must stop if you still can.
- Red: Brake! Red means you must stop and absolutely may not drive on.
Although nobody really needs this memory refresher, it helps a lot in explaining TDD. In Test-Driven Development we basically do exactly the same. Only each colour means something slightly different, and TDD has no amber, but a completely different, crucial third step.
Phase 1: Red (the failing test)
This is the first colour you see when you write a test in TDD. That is because with this method you always start by writing the test, and only then write the actual code.
You make a blueprint of what you are going to test, and only then do you fill it in. Just like an architect first makes a drawing before the contractor starts building. Better still: building can only start once the blueprint has been approved (the acceptance criteria).
Because you have not written any underlying code at the start, the compiler does not know what it is looking at and immediately returns errors. With an error, the test does not pass: it fails. So red means: you cannot go further yet.
Phase 2: Green (making the test pass)
Once you have written the blueprint of the test, you make red turn green. You do this by using as little code as possible to make sure the test does pass.
The scenario
First we create our test. This test is very simple: we have a list of numbers and we want to sort them from high to low (descending). Sounds easy, right?
Swift
class TDDBlogTests: XCTestCase {
func testSimpleSortingDesc() {
let numbers = [5, 6, 3, 2, 1]
XCTAssertEqual([6, 5, 3, 2, 1], Sorter.sortDesc(numbers))
}
}
The minimal solution
Following the TDD method, all we do now is make this specific test green. We are not looking at the broader acceptance criteria yet. To do this, we first create a Sorter with a function called sortDesc that accepts a list of numbers.
In the code example below, you can see that we return the list directly in the hardcoded 'correct' order:
Swift
enum Sorter {
static func sortDesc(_ numbers: [Int]) -> [Int] {
return [6, 5, 3, 2, 1]
}
}
Ta-da! Now the test turns green. The list is returned exactly as the test expects.
Of course, this only works for this specific scenario. If we need to sort other lists in the future, the test will turn red again straight away. That is why we need to move on to the last step of the TDD principle. Unfortunately, this step has no colour of its own on the traffic light, but it is the most important one: Refactor.
Phase 3: Refactor (optimising the code)
The refactor step is the most important step in making sure your application really works as it should in the end (and that is what everyone wants, of course).
In the refactor step, we optimise the code and make sure it really meets all the acceptance criteria. In our simple example it does not amount to much yet, but for more extensive features that have to meet many requirements, this is quite a big and crucial step.
The final code
If we now want our Sorter to always sort any list correctly in descending order, we need to adjust our function as follows:
Swift
enum Sorter {
static func sort(_ numbers: [Int]) -> [Int] {
return numbers.sorted { $1 < $0 }
}
}
We will not go too deep into the Swift code itself here, but this logic makes sure that every list you pass in from now on is always sorted neatly from high to low. The test stays green, and the code is ready for the future!

Ready, test, go!
Test-driven development lets you develop software of the highest possible quality. Are you thinking: “Hey! I like this way of working.” Then take a look at our vacancies.
Looking for an app developer who can take development to the highest level together with you? Then let's have a cup of coffee.
.avif)




