DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 968,547 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Isaac Ayodeji Ikusika
Isaac Ayodeji Ikusika

Posted on • Updated on

Test Driven Development - Explained

Test Driven Development (TDD) in layman's language is a way of testing a piece of code before writing the actual code.

The first time I saw this explained, I was confused. It was completely abstract to me - How or why would I test a code I have not written? I think I felt this way because most of the research I did concerning this topic was always about adding two numbers or did not use [near] real examples, so I'm going to use my story to explain this term in this article.

I used to work in a company that writes API for financial companies, so I was given a task - display all active customers that have more than $400 in their account.
Easy task! I wrote the code:

var activeCustomersWith400 = Customers.Where(cus => cus.IsActive && cus.Balance > 400)

//pardon my C# and LINQ 😏
Enter fullscreen mode Exit fullscreen mode

Yes, that's correct, then I wrote the test for it after manually testing/making sure my code works - by debugging. That is not TDD approach.

TDD approach is:

  • Creating test methods (or a class with methods) specifically for that task to:
    • test that the customers returned are active
    • test that they have $400 in their account
    • any other test you want, making sure they go along with the requirements of the task.
  • Running the tests. They will obviously fail, or your IDE or compiler complains about errors, since you do not have the function that does the task in your actual code yet.
  • Fix the errors by writing the function, then run the test again. Keep running the tests and making changes to your function until all the tests pass.

In conclusion, TDD approach is better than just jumping in the code because:

  • It defeats the need for debugging. This is because you already laid down how you want your code to behave in the tests, you just write code to follow that behavior. This is the strong point of TDD.
  • It makes you think about small chunks of code at a time. This is required in functional programming.
  • It improves productivity because developers have to focus on that part of code they are working on.
  • You do not need manual testing to know your code works.
  • One important advantage of TDD to me is how it helps in Behavior driven development (BDD).

Some people argue that TDD wastes time, maybe they are right, but to me, the advantages listed above outweigh this argument and I can say for a fact that it does not waste time, especially when you have your test setup ready from onset.

Thank you for reading.

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Just kidding, it's a personal preference. But you can change your theme, font, etc. in your settings.

The more you know. 🌈