Sean Coughlin
Sean Coughlin's Blog

Sean Coughlin's Blog

What is TDD?

Photo by David Travis on Unsplash

What is TDD?

Test-driven development (TDD) is a programming style where coding is preceded by testing.

Sean Coughlin's photo
Sean Coughlin
·Mar 26, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • The Rules of TDD
  • Benefits of Test-Driven Development
  • Cons of TDD
  • Further Reading

Test-driven development, or TDD, is a programming style. TDD focuses on improving code by focusing on writing tests first. This is the opposite of typical software development. In most cases, the software is developed first, and test cases are created later.

The Rules of TDD

The general principle follows just three steps:

  1. Write one unit test describing an aspect of the program. The test should fail upon running because the program lacks the needed feature.
  2. Write the minimal amount of code possible that will solve the test. The goal should be to keep things simple. We only need to make the test pass.
  3. Refactor if necessary to reduce duplicate code or improve quality. Then continue on to repeat the process.

Benefits of Test-Driven Development

  • Test coverage is extremely high because code is written to make test cases pass. This ensures good code quality and keeps defects to a minimum.
  • Writing only the very basics to make a test pass keeps code simple and the design cleaner.
  • Defects are caught earlier because the test cases are already written.

Cons of TDD

  • TDD is a design pattern, so teams can implement TDD differently. Even within teams developers can have various levels of adherence to the principles.
  • TDD can lead to an over-focus on small code chunks and less focus on wider functionality tests.
  • Testing becomes part of the project and maintenance can be high if tests are written too rigidly.

Further Reading

TDD is closely related to ATDD or acceptance test-driven development and BDD or behavior-driven development.

Did you find this article valuable?

Support Sean Coughlin by becoming a sponsor. Any amount is appreciated!

See recent sponsors | Learn more about Hashnode Sponsors
 
Share this