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.

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 writte…


This content originally appeared on DEV Community and was authored by Isaac Ayodeji

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

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 made concerning this topic were 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 😏

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 chunk 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 outweighs 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.


This content originally appeared on DEV Community and was authored by Isaac Ayodeji


Print Share Comment Cite Upload Translate Updates
APA

Isaac Ayodeji | Sciencx (2021-11-04T04:45:29+00:00) Test Driven Development – Explained. Retrieved from https://www.scien.cx/2021/11/04/test-driven-development-explained/

MLA
" » Test Driven Development – Explained." Isaac Ayodeji | Sciencx - Thursday November 4, 2021, https://www.scien.cx/2021/11/04/test-driven-development-explained/
HARVARD
Isaac Ayodeji | Sciencx Thursday November 4, 2021 » Test Driven Development – Explained., viewed ,<https://www.scien.cx/2021/11/04/test-driven-development-explained/>
VANCOUVER
Isaac Ayodeji | Sciencx - » Test Driven Development – Explained. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/04/test-driven-development-explained/
CHICAGO
" » Test Driven Development – Explained." Isaac Ayodeji | Sciencx - Accessed . https://www.scien.cx/2021/11/04/test-driven-development-explained/
IEEE
" » Test Driven Development – Explained." Isaac Ayodeji | Sciencx [Online]. Available: https://www.scien.cx/2021/11/04/test-driven-development-explained/. [Accessed: ]
rf:citation
» Test Driven Development – Explained | Isaac Ayodeji | Sciencx | https://www.scien.cx/2021/11/04/test-driven-development-explained/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.