TEST DRIVEN DEVELOPMENT

Ravi Chandola
Javarevisited
Published in
2 min readMay 9, 2024

--

Photo by CDC on Unsplash

Test-driven development (TDD) is a way of writing software where you write the tests for your code before you actually write the code itself. Imagine you’re building a model airplane. Instead of just gluing parts together and hoping it looks like the picture on the box at the end, you start by creating a checklist of what you want the airplane to look like and what it should be able to do. Only after you have that checklist do you start building the airplane, constantly checking off items as you go.

In TDD, the “checklist” is a set of tests. Here’s how it typically works:

  1. Think about what you want to do. Before you write any code, you think about what function or feature you want to add to your software.
  2. Write a test for it. You write a test that checks if that function or feature works. But you haven’t written the code for the function yet, so of course, the test will fail. This is a bit like setting a goal that you haven’t achieved yet.
  3. Write the code to make the test pass. Now you write the actual code that will make the test you wrote succeed. This is like doing the work to hit your goal.
  4. Run the test. You run the test again, and if you wrote your code right, the test should now pass because the function does what it’s supposed to do.
  5. Clean up your code. Once the test passes, you can clean up your code, making sure it’s nice and tidy, without worrying about breaking it. Since you have the tests, you can always check to make sure everything still works after you clean up.
  6. Repeat. You repeat this process for every new piece of functionality you want to add, building your software piece by piece and ensuring with tests that each piece works properly.

The benefits of this approach are that it helps you focus on what your code is supposed to do, it gives you a way to check your work immediately, and it often leads to better designed, more reliable software. It’s like having a safety net that catches you every time you make a mistake, so you can fix it right away instead of finding out much later.

--

--

Ravi Chandola
Javarevisited

As a technology enthusiast, I have a passion for learning and sharing my knowledge with others. https://www.linkedin.com/in/ravi-chandola-304522133