Embracing Test-Driven Development

“Do The Simplest Thing That Could Possibly Work”
Kent Beck

Sometimes when we are trying to write down complicated business logic into code, it takes us several hours to validate if the implementation is under the requirement. Furthermore, if we find out that there are bugs in our code and we don’t exactly know how can it happen. These are some frustrating situation you may want to consider Test-Driven Development.

What is Test-Driven Development?

Test-Driven Development (TDD) is a practical method that guides software engineers to write code implementation based on the test that they have created before. The term “Test-Driven Development” dates back to the 1990s as it was part of Extreme Programming, a software development methodology proposed by Kent Beck.

The main idea of TDD is simple. You can only start writing the code implementation after you write the test and only on the scope of the test. Then, after you are sure that there is no error occurred in the test set, you are allowed to write another test. Therefore you are aware and in complete control of the current state of your program.

How to implement it?

As we already know, We need to write the test case first before implementing the code. There are three main phases for conducting Test-Driven Development. Those steps are writing a failed test case, implementing the code to pass the test, and refining or refactoring the code. We repeat the phases until the program reached our goal. Look at the graphic below to understand TDD better.

Test-Driven Development Cycle
Image Source: https://www.researchgate.net/figure/Fundamental-TDD-Cycle_fig1_254008456

1. Write Failed Test Case [RED]

In this first phase, your primary goal as a software engineer is to write a specific test case for certain functionality or feature. The produced test case must be able to get compiled by software engineers and fail the first time it runs. The failed test case’s purpose is to give you a sense of how the implementation works in the future.

What if the test case itself is confusing? There is a concept to help software engineers to write clean codes. That concept is F.I.R.S.T. It is an acronym that stands for Fast, Independent, Repeatable, Self-validating, Timely. The explanation of each principle is explained below:

  1. Fast
    Test suites (collection of test cases) must be able to run quickly. The idea is that so software engineers are not disinclined to execute the test suites.
  2. Independent
    Each test case in a test suite must not be dependent on another test case. If the test cases are so reliant on each other, one failed test could cause a series of failures in the test suite.
  3. Repeatable
    The test cases must be able to run in any environment They must be able to run in your local environment, production, QA, etc. If they don’t have that ability, you will not be able to run test every time you want.
  4. Self-Validating
    Every test case must only have two options of outputs, either pass or fail. The goal is to prevent subjective errors in the failure state of a test case. Subjective errors occur because of manual testing.
  5. Timely
    Every test case must be available before the production code so that the results of the production code are testable.

2. Implement The Code To Pass The Test Case [GREEN]

This phase is your time to develop the implementation. The implementation must be as minimum as possible just to pass the test case you created. Make sure you don’t develop an overkill implementation to prevent missed code part to be tested.

3. Refined your code [REFACTOR]

The last phase is for you to make your implementation cleaner. Your goal is to reduce duplication and make sure your code is readable so that other software engineers could comprehend it easily.

The Great Benefits of TDD

  1. The end result of Test-Driven Development is a maintainable code. Your code will be easier to read, clean, and manage at the same time. It correlates with the clean code concept.
  2. The cost of project will be decreased. Since TDD will result maintainable code, you will have less money spend on bug fixing.

But, There are also drawbacks..

  1. You maybe understand the concept of TDD but when you learned a new programming language, TDD concept is beyond understanding to implement. You must be able to utilize programming language first before you implement TDD.
  2. It will require more time to develop implementation. Since you need to make test case first before you can write the implementation, you will find your time to code is more than usual.

References:

https://www.researchgate.net/figure/Fundamental-TDD-Cycle_fig1_254008456

--

--

--

Hello! Good day to you! You can call me Haikal. I am curious person interested in data science and software engineering.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Your Project Outsourced #4: What Are The Different Types Of Outsourcing Contracts & How To Choose…

Your Project Outsourced #4: What Are The Different Types Of Outsourcing Contracts & How To Choose The One For You?

Windows 10 → Microsoft Store blocked by admin? Need to re-endable? Try this.

How to connect to the Ethereum network with a client written in Java

Pangeo with Dask Gateway

[DevLog] #3 UnnyNet v3.0

My Experience on LetsGrowMore

The Best Piece of Advice I Got When I Was a Junior Software Engineer

Store a sensitive kubeconfig with Kubernetes and clouds providers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Haikal Susanto

Haikal Susanto

Hello! Good day to you! You can call me Haikal. I am curious person interested in data science and software engineering.

More from Medium

Automate Prettier, ESLint using Husky and Lint-staged

Real Users, Real Clients at Encora Apprenticeship — Week 3

Using Test-Driven Development (TDD) in Agile Development

Today is not about writing code — Today is all about ‘Velocity’