Test Driven Development: Example

Hicham BEN KACHOUD
5 min readJan 19, 2021

--

As i promised all of you when i created my first article Introduction of TDD, today i’m going to show you a basic implementation of a simple example.

NB: I am not a master of TDD but i can help with my basics knowledges.

1- From Previous Article:

A programmer taking a TDD approach refuses to write a new feature until there is first a test that fails because that feature isn’t present. In fact, they refuse to add even a single line of code until a test exists for it. Once the test is in place they then do the work required to ensure that the test suite now passes.

2- Example:

This is a simple example with a minimum of conditions, because for a big example we need to record it on a video, it will be good than article.

A sports club offers 3 categories of price for members. Each category depends on the Member’s situation.

1- A member under 18 years old and having a guarantor will have the category Bronze, otherwise he will be refused.

2- A member above 18 years old and having a seniority of less than 2 years in the club will have the category Silver.

3- A member above 18 years old and having a seniority of more than 2 years in the club, will have the category Gold

3- Implementation:

You should use a simple php application with phpunit installed or install a simple symfony application.

I used symfony microservice application.

  • Step 1:

Given: A member under 18 years old and having a guarantor

When: getting the category of price

Then: category will be bronze

==> Create Test 1

first test that is going to fail

If you run the test using the command ./bin/phpunit it is going to fail.

why? we don’t yet have a line of code.

  • Step 2:

We should make this test work, So we need the minimum of code.

Code to make first test work

If you run this test you will get a green color :). It works.

what??? wait??? where is the logics, conditions????

So this is the magic of TDD, we create the code that makes our test work. For example above, all we need is that our method (execute) to return a bronze category.

  • Step 3:

Given: A member under 18 years old and doesn’t have a guarantor

When: getting the category of price

Then: category will be null

==> Create Test 2

Test 2

If you run this test, it doesn’t work.

Test 2 failed

let’s make it work :)

Code to make Test 2 Work

Now, if you run the command, all tests work.

  • Step 4:

Given: A member above 18 years old and less than 2 years of seniority

When: getting the category of price

Then: category will be Silver

==> Create Test 3

Test 3

Run this Test and it is going to show You a RED color :D (Not bad)

let’s make it work my friends,

Code to make Test 3 work

So my friends what do you think? Like I said before, TDD is not for testing your application, it is about driving your development with a minimum of code.

I think we don’t need a refactoring yet :D

  • Step 5:

Given: A member above 18 years old and more than 2 years of seniority

When: getting the category of price

Then: category will be Gold

==> Create Test 4

Test 4

Run the command and it is going to fail.

Test 4 failed

We should make it work. ( I hate red color :D)

Code to make Test 4 work

Now, If you run the command, you will get all tests work. ( I love Green Color ❤)

All Tests work

4- Refactoring:

For this sample example, i don’t think that we need to do some refactoring, but if one day our PO (Product Owner) needs to add more complicated conditions (Dev iterative and incremental), so what we are going to add multiple conditions (if … else if … else if …… else) in the method execute.

But what if we add a new category Master, with multiple conditions, and one day after, we add another new category, what??? our method is goint to die :(

i did some refactoring, I created a class for each category type which implements an abstract class with a simple method isValid.

Each class had the logic inside the method isValid that satisfied the category type. Using this approach, it will be easy to add a multiple categories.

I added also a CategoryService class which verify and get the correct category.

So our method execute inside our use case contains:

Refactoring

5- Conclusion:

Maybe it is not a great example, but it is basic and simple to understand how TDD it works. This is how i code in my projects and i invite all of you to use it.

You can contact me for questions & information on LinkedIn:

You can find the example here.

--

--

Hicham BEN KACHOUD
Hicham BEN KACHOUD

No responses yet