TDD for IOS App Development Tutorial

Let’s learn about Test Driven Development with a simple example.

Bobby Pehtrus
5 min readAug 23, 2021

Lately I’m trying to learn on how to do TDD when building apps. But, writing tests is still not common for me. But I’m going to try my best to document what I learnt and implement it on this blog.

I’m going to explain what is Test Driven Development is in general and practice it using swift for IOS with a simple login feature. Writing test is an important task that is heavily neglected by most of the IOS Developer now and this practice should help creating a new culture on writing code with tests at the same time.

If there’s something I missed, please let me know, its wonderful if there’s someone who can back you up if you make mistakes!

Steps on doing TDD

  1. Add a test.
  2. Run all test and see if there’s any test fails.
  3. Write some code.
  4. Run the test again and Refactor.
  5. Back to step 1.

The TDD is a repetitive methodology to test first then code. The test cases will grow larger and larger as long as we keep adding more features.

Step 1 : Writing A Test

So let’s start by creating a new project in XCode. I named it Weathery. If you happen not include the test targets on creating the project you can simply add it with File -> New -> Target ->Unit Testing Bundle.

We will given a new target called WeatheryTests and a test file that subclassed XCTestCase. Here’s where all the test begin.

So I’m thinking about testing the Login feature in this blog. First step, what am I going to test? Well, the Login feature will include email and password validation on login. The validation will be placed under a LoginViewModel.

LoginTests.swift

SUT stands for “System Under Test”

Step 2 : Run the Test

Well… you can’t run it, just yet.. There’s is 2 possible reason why the build has failed. First, check your imports! Make sure you import your project module to the test module by using @testable import YourApp .

Remember to import your main module!

The second reason is, you simply don’t have the LoginViewModel in your app. And in this case, that’s the reason. Let’s jump to the next step!

Step 3: Let’s write some code

Let’s create our LoginViewModel.

class LoginViewModel {
// Don't know what to do
}

Well the LoginViewModel still a dumb class that doesn’t know what to do. As you code, you will realize one by one the error is disappeared.

Yet another error.

The errors now jumps to the login method. As you can see, we are being guided by our own test!, how cool is that! We still don’t have the login method, so lets make one!

LoginViewModel.swift — login and validation

If you want to see the validator you can check it out in my gist here!.

Now the errors are gone! Let’s…

Step 4 : Run the test and Refactor

Run the test from Build Button

You can run the test by pressing cmd + U or run the test using the Build button on top left. Hover on the button and press the Test menu.

Now you can see, a happy green ✅ over there. Yes it is because I’ve build the Form Validator earlier. And while I’m building it, I’m going trough a series test and code cycle. Pressing cmd + U repeatedly and make the test work.

I REALLY DO RECOMMEND TO CODE ON YOUR OWN FIRST

Why?? Because while you code, you will make mistakes, and the mistakes will be captured by the Tests that you have made. It will show the Red ❌ mark. This is where the art of TDD shows its color. You go one by one case, one may succeed one may failed. It guards you from creating the same mistakes or breaks another code. A wise programmer I watched on youtube said:

If a test fails, you make progress.

So, write a failing test is a very important task to do!

Refactoring is a code clean up. You can try to evaluate your code with SOLID principles here. Maybe your code is not doing SRP right, or you wonder why my constructor has many parameters. Is this app modular enough? Is there any reason that I have to modify this code if I add another feature?

Think forward, but don’t overthink it, it is because an app keeps changing.

You may implement design patterns here with ease, adding Factories, Strategy Pattern, adding Adapters or maybe Compose your dependencies. You may change your code structures a lot.

And you will have confidence doing it. WHY? Because you have a reliable tests that keeps you safe from breaking any features.

Step 5 : Repeat.

Write a failing test, add some code, test it again, refactor it, and repeat.

Conclusion

The Test Driven Development is hard to do at first. It is still a difficult task for me today. But I believe, when you try to do it over and over again, implement it on every project that you work on, I’m sure it will become a routine.

I think, creating tests adds more value in you. As an IOS Developer it is a necessity to keep your software “soft” and “safe”. That’s what differs between regular developer and expert ones.

I will keep the Weathery repo on github. It my changes overtime while I’m learning TDD, SOLID and implementing design patterns. I’m really appreciate if you want to help me learn to become a real pro IOS Developer.

I recommend watch this playlist if you feel lack of explanation or examples. I really inspired by them!

Wish you all good health and a good food 🍲 🍱!

--

--

Bobby Pehtrus

Nomad Developer, Loves to travel and would walk for a coffee.