Software Development with Test Driven Development

Faza Aryoga
Electronic Logbook
Published in
3 min readMay 23, 2021
Photo by Charles Deluvio on Unsplash

Oftentimes, programmers nowadays are taught Test Driven Development or TDD for short. An approach to software development wherein developers first create tests according to the software’s requirements and then create the implementations to pass the tests. The idea is to avoid problems later down the line during development whereby creating tests for the all the smallest functionalities in the beginning and changing your implementations to pass the tests, avoids code duplication and other problems down the line.

How to do TDD

The general steps of Test Driven Development can be summed as:

  1. Write test
  2. Run all written tests and see if new ones fail
  3. Write code
  4. Run tests and refactor the code
  5. Repeat
Flowchart for TDD. Source: https://www.guru99.com/test-driven-development.html

The common mistakes that usually happens in TDD is that writing too many tests at once or ones that are too large. The correct way is to create a unit test, then write just enough code to pass the test, refactor, and then on to creating the next tests, rinse and repeat.

Levels of TDD

There two levels of TDD

Acceptance TDD

In Acceptance TDD or ATDD, we write a single acceptance test. This test fulfills a requirement of the specification or a behavior of the system. And then we write just enough production/functionality code to pass the test. ATDD is also known as Behavioral Driven Development (BDD).

Developer TDD

In Developer TDD we write a single developer test i.e. unit test and write just enough code to pass that test. The unit tests focus on the smallest functionalities of the system. Developer TDD is simply known as TDD.

Both ATDD and TDD aims to specify detailed, executable requirements for your system on a just-in-time basis (JIT). JIT means taking only those requirements in consideration that are needed in the system.

Advantages of TDD

- Early bug notification.

- Better designed, cleaner, and more extensible code.

  • It helps to understand how the code will be used and how it interacts with other modules.
  • Results in better design decision and more maintainable code.
  • TDD allows for writing smaller code that has single responsibility rather than monolithic functions with multiple responsibilities which also makes the code easier to understand.
  • TDD also forces code to be written to pass tests based on user requirements.

- Confidence to refactor

  • When refactoring code, there are possibilities of errors in the code. Having a set of automated tests allows you to fix those errors before release as proper warnings will be given when tests are run.
  • Using TDD should result in code with fewer bugs, faster, more extensible, and can be updated with minimal risks.

- Good for teamwork

  • In the event that a team member is absent, other team members can pick up the work easily on the code. Also aids in knowledge sharing, thereby increasing team efficiency.

- Good for developers

  • By using TDD, even though we are spending more time creating test cases, it will take less time for debugging and developing new features. We will also write cleaner, simpler code.

--

--