TDD For Beginners - Illustrated With An iOS Example Using Swift

Ravi Kiran HR
5 min readMar 18, 2022

--

In any platform, projects will grow bigger over the time and becomes complex. One important strategy to manage this complexity is through the use of unit tests. By writing unit tests, a developer can point out the intention of the code and provide a safety net against the introduction of bugs.

With the unit tests first approach (TDD), the developer focuses on the problem. This way, they are forced to think about the domain and rephrase a feature request using their own understanding by writing the unit test.

As a result, the code looks modular, testable, readable and the developer gains more confidence on the code.

In this article let’s understand what is TDD and what are the benefits of it over traditional unit testing approach and a hands-on example to understand how to apply it on iOS platform using Swift programming language.

Download Materials.

You can grab the complete source code of the demonstration from the below GitHub link.

https://github.com/kiranRavi/TDD-iOS-Platform.git

What Is TDD ?

TDD is an agile software development methodology that makes development more bug-free and makes the code more stable by writing test cases for the units of business logic upfront. It is a popular and modern way to write software. It was created as part of the Extreme Programming (XP) methodology.

TDD Vs Traditional Unit Testing.

TDD Vs Traditional Unit Testing.
TDD (VS) Unit Testing

How TDD Works?

It has 3 main steps/states.

Red: Write a failing test case.

Green: Write a minimal code to make the test pass.

Refactor: Refactor both test case / code.

1.Red: You start by writing a failing test. It needs to test a required feature of the software product that is not already implemented or an edge case that you want to make sure is covered. The name red comes from the way most IDEs indicate a failing test. XCode uses a red diamond with a white x on it.

It is very important that the test you write in this step initially fails. Otherwise, you can’t ensure that the test works and really tests the feature that you want to implement. It could be that you have written a test that always passes and is, therefore, useless. Or, it is possible that the feature is already implemented. Either way, you gain insight into your code.

2.Green: In the green step, you write the simplest code that makes the test pass. It doesn’t matter whether the code you write is good and clean. The code can also be silly and even wrong. It is

enough when all the tests pass. The name green refers to how most IDEs indicate a passing test. XCode uses a green diamond with a white check mark.

It is very important that you try to write the simplest code that makes the tests pass. By doing so, you only write code that you actually need and that is the simplest implementation possible. When I say simple, I mean that it should be easy to read, understand, and change. The code should always be easy to understand.

3.Refactor: During the green step, you write just enough code to make all the tests pass again. As I just mentioned, it doesn’t matter what the code looks like in the green step. In the refactor step, you should improve the code. You remove duplication, extract common values, and so on. Do what is needed to make the code as good as possible. The tests help you to not break already implemented features while refactoring.

TDD Explained With An Example.

Let’s build a ‘MailComposer’ class with composeGreeting() function ,where this method should return an appropriate greeting message, depending the following requirements:

  1. If first name and last name both are nil, it should return nil.
  2. If only first name is present, it should return a friendly greeting message such as “Hi <firstname>.”
  3. If only last name is present, it should return a formal greeting message such as “Hello, Mr.<second name>.”
  4. If both first name and last name are present, it should return a full greeting message such as “Good to see you, Mr. < first name> + <second name>.”

Let’s practice TDD on iOS platform using Xcode / XCTestCase framework.

Step 1. Write a failing testcase.

Write a test case which fails , we are going to write a test testComposeGreetingWhenBothFirstSecondNameNil which will invoke the method composeGreeting(firstName, lastName).

When we run this our build fails with the following error on the function. whenComposeGreetingInvoked()

We have reached the first Red State, now let’s add a minimal code to make the test pass.

Step 2. Write a minimal code, to make the test pass.

Now when we re build-run the project, our failing test case should pass.

Step 3. Modify the test case, to meet the further requirement expectations.

Now when we re build-run the application, build fails with the error.

Step 4: Repeat step 2.

i.e Write minimal code to make the test pass, and the test passes.

And, incrementally when we meet all of the above mentioned requirements, our implementation should look like below.

And the complete test suite should look like below.

Summary.

Test-driven development (TDD) is a development technique where you must first write an unit test that fails before you write new functional code, TDD ensures to think end to end design upfront, gives better code coverage compared to traditional unit testing. And gives a documentation for the design/architecture. TDD makes the code modular and simple.

In this article we have learnt what is TDD (Test-driven development) and how it differs from the traditional unit testing approach, and a demonstration on iOS platform.

References.

• Pluralsite http://www.butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd.
• https://leanpub.com/tdd_ios_gimme_the_code
• https://qualitycoding.org/ios-tdd/
•https://r-stylelab.com/company/blog/web-development/from-tdd-to-bdd- when-to-switch-and-how-to-apply

--

--