Test Driven Development: Making sure your code is doesn’t sh*t itself to much.

What is Test Driven Development?

Basically, Test Driven Development (TDD) is an approach to the way we code in which tests are written to validate and decide what the code will do. In other words, write tests first then implement code that passes the tests. If the tests still fail, then write new code. In doing so the way the code is written is much more structured and organized.

Source: What is Test Driven Development (TDD)? Tutorial with Example (guru99.com)

How to TDD?

The how is pretty simple, just follow these few steps:

  1. Create or add some tests
Here I wrote some tests for “Lampiran” model
Here I try and run the tests. The tests failed because I haven’t implemented “Lampiran” model yet.
Here I implement Lampiran Model according to needs.
After I implemented the models, the tests are now passed!
Here I write the next tests for the next functionality, in this test I plan to develop a serializer for the model.

An Example of Implementing TDD

Currently I am working on a group project to develop a mobile application and we use TDD on approaching it. So how do we do it?

  1. Pick a functionality/requirement that needs to be met.
Example of Commit message for [RED]
Example of commit message for [GREEN]
Example of commit message

Why you should use TDD in almost any project?

There are many advantages to using TDD during software development.

  1. Minimizes bugs and errors in the program/application
    Usually TDD results in more tests being written because we are much more thorough about the code we write. More tests means more code coverage and less likely to come across a bug that isn’t covered in the code.
  2. More focused coding
    From the process I explained earlier, when writing a test we pick a functionality beforehand. So during an iteration, we are more focused on finishing that functionality and less like to wander off to other functionalities that also need to be met.
  3. Modularity
    Because of the “pick one functionality” mindset from this cycle, the code we write tends to become less dependent on other code (or modular). This helps prevent programmers from breaking the program when writing the program.
  4. Safe refactoring
    When refactoring, we have a safety net of tests to ensure when we make a mistake while refactoring, we can catch the mistake after running the tests. This also prevents programmers from accidently breaking the program when refactoring something. Preventing any mistakes like the ones I made in my initial story.
  5. Tidier Code
    Because TDD causes the code to become more modular, the code we write tends to be more tidier as well.



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