• 20 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 4/27/23

Discover Test Driven Development (TDD)

So far, we've covered some aspects of testing that demonstrate benefits in the development process and improve the quality of your application. You can use this approach to verify that the code you wrote is valid (and amazing!).

Now, we are going to go further and learn how to create bug-free code from the moment of writing. Does that sounds like science fiction? πŸ›Έ I'm serious though, and it's possible!

If tests can provide us that guarantee, then we surely need to write tests! ...Even before we write the code!

BEFORE THE CODE?! 😱 

Exactly! Create tests before the production code!

How do we do that? πŸ˜†

Introducing Test Driven Development (TDD)

The development technique of writing the tests before the production code is called Test Driven Development, or TDD for short.

How does that work? πŸ€”

Imagine you are working on an app that creates reminders and you are asked to implement an ability to delete a reminder.

You have two options:

1. Write more code!

You can dive into the existing code, spot parts where new functionality should be added and start typing envisioned implementation. Periodically you'd hit Run, use some sample data and perform deletion to make sure it works as desired.

Wait, maybe in your company, you are required to accompany your code with tests. Fine, let's write some tests - you just checked it works anyway! πŸ˜‰

2. Following TDD

Alternatively, you can implement a TEST FIRST that checks deletion. At this point, of course, the test fails because you haven't even created a corresponding function.

This gives you a single task: fix the failed test. In doing so, you develop the missing functionality. When you are done, without any manual test, you know it works, because yourtest just passed!

Let's illustrate:

TDD Objectives

What's the purpose of this technique?

There are many good reasons to follow TDD. Here are some of them:

Encourage you to think through your code design πŸ€”

By writing the tests first, you are forced to think about what you want your code to do before writing it. You have a clear idea of ​​the result you want to achieve. This will definitely improve the quality of the result and save you time.

Create a healthy development environment πŸ€

When developing in TDD, your tests are constantly present and up-to-date. This allows you to navigate your code with confidence, because you know that if you cause any bugs, a test will notify you immediately.

Keep you focused πŸ‘©πŸ’»

It's easy to get carried away when writing code. This could be anything from trying to find a perfect solution to attempting to pack too much functionality into a class or method. This can cause at best a waste of time and fatigue and, at worst, bugs!

When you develop in TDD, you write a single test that fails and then you fix it by developing a corresponding feature. This makes you do one SMALL thing at a time.

If you start jumping around, your tests will tell you: they'll turn red one after another. 😑😑😑 And since our brains associate red with danger and alert, we have little tolerance for it. Hence, we are wired to eliminate such warnings before adding on!

Validate code automatically βœ…

When you develop a feature, you do not even have to manually test it. Your tests take care of it automatically for you! No manual validation to do.

Maintain high standards ⚠️

It's good to have high standards in everything, your work included. Tests ensure quality, so they are important! Engraving this in your mind will allow you to maintain high quality of work.

Focusing on tests would lessen the quality of production code, wouldn't it?

Of course not, there's no risk to the quality of the production code as the main purpose of the tests is to ensure the quality of the production code. By attending to tests first, you always ensure the high quality of your production code!

So, write tests first!

Let's Recap!

  • Test Driven Development (TDD) is a very powerful development technique in which tests are written before the code.

  • In particular, written tests are meant to fail in the beginning, which forces the correct production implementation in order to fix the test.

  • TDD objectives are the following:

    • Encourage you to think through your code design.

    • Create a healthy development environment.

    • Keep you focused.

    • Validate code automatically.

    • Maintain high standards.

Ever considered an OpenClassrooms diploma?
  • Up to 100% of your training program funded
  • Flexible start date
  • Career-focused projects
  • Individual mentoring
Find the training program and funding option that suits you best
Example of certificate of achievement
Example of certificate of achievement