TDD: repeat and refactor

With test-driven development, you start with a failing test and focus on getting it to pass.

But what do you do once it's passing?

You either repeat the process, adding more assertions to the same test until you generate the next failure or write the next failing test, or take the opportunity to refactor the code.

Is there something you'd like to change within the implementation or test code? You can do that whilst the tests are passing.

You may want to extract a new class or method to improve the implementation or create some test helpers to reduce duplication within the tests.

You just need to keep the tests passing to ensure the code still works. If something breaks, you fix it as soon as possible, either by fixing the code or reverting to the previously passing code.

Then, once you've completed the refactoring, move back into the red/green phase with the next test failure.

- Oliver

Was this interesting?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.