DEV Community

Agnes Reina
Agnes Reina

Posted on

My experience with Test Driven Development (TDD) and how to implement it in a project

Introduction

Test-driven development (TDD) is a development technique where you first have to write a test that fails before writing a passing code. It relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the code is improved so that the tests pass.

“For my first-time using TDD, I asked myself why should we write code to test other code that we don’t even have.`”

TDD starts with designing and developing tests for every small functionality of an application. It ensures that the source code is thoroughly unit tested and leads to modularized, flexible and extensible code. It focuses on writing only the code necessary to pass tests, making the design simple and clear.

Activities involved in Test-Driven Development (TDD)

1. Coding

The first activity that you do in TDD is to write a unit test case which is a single test that describes a feature of a program. To write a test, the developer must clearly understand the feature's specification and requirements. The developer can accomplish this through use cases and user stories to cover the requirements and exception conditions.

2. Testing

Run the test and see if all test cases now pass, the programmer can be confident that the new code meets the test requirements, and does not break or degrade any existing features. If they do not, the new code must be adjusted until they do. And then you write the simplest code possible to make the test pass.

3. Refactoring

Refactoring is an activity where you make the code efficient. You do not change the functionality, but rather alter the internal dynamics to enhance the performance. Some examples include identifying and removing duplicate and dead pieces of code, simplifying nested classes.

The benefit of using TDD

With TDD you get fewer bugs in code because you have tested out the main features. The other thing is the focus, what I am trying to say here is that your focus is narrowed to making the test passes. The third benefit of using TDD is that test act as documentation if you don’t know what a bit of code do you can read it and see what it does.

Conclusion

In test-driven development (TDD), a test is written first before writing the functional code. This doesn’t mean that TDD can replace traditional QA, but instead, it ensures effective unit testing. An effective unit test will ensure better code coverage.

Top comments (0)