How do you know which test to write next? How do you make sure you make progress towards your goal, that is, towards implementing a feature that is useful to your customers?
Intro (TL;DR)
When doing test-driven development - or TDD - you must must always choose the next test wisely: It should be small and fairly easy to write and also not too hard to get from red to green. And it should bring you closer to finishing the current feature.
How do you choose that next test? By thinking before you even write the first test.
I am David, a technical agile coach and trainer with over 12 years of experience. If you like my videos, please subscribe to my channel and share them with your friends and followers using the share buttons on the left.
Question / Problem
When I teach trainings, I sometimes see people either
- Get stuck on a single test, where they do not know how to proceed OR
- Write a lot of tests in a red-green-red-green-… fashion but not get any closer to their goal
In both cases, people are getting stuck because they did not choose a good next test. In the first scenario, the test and implementation were too big. In the second, they choose things that are easy to test, but not really important right now.
Solution
When you get stuck like that, do not just continue. Do not try to dig yourself out of the hole. Take a short break, then go back to the drawing board.
Make a list of things your system under test should do. Suppose we want to develop a simple hangman game. Write down everything it should do, then sort that list.
- Shows a placeholder at the beginning of the game
- Updates the placeholder after every correct guess
- Player has won when the word was guessed correctly
- Player has lost after 11 incorrect guesses
- Counts number of incorrect guesses
Choose the next item from that list that is both easy to implement right now and relevant to end users. Like “Shows a placeholder at the beginning of the game”. Think of the simplest possible test you could write.
- Placeholder is a single _ when the word to guess is “a”
After this test is green, choose another test that helps you finish this item.
- Placeholder is “_ _ _” when the word to guess is “the”
And so on. Your list of features / behaviors helps you choose the next test. And when a test comes to your mind that you want to write later, write that down too. You can, for example, create a test list at the end of the file that contains your tests, with comments or disabled tests for future tests you want to write.
Conclusion
To decide which test to write next, create a list of things your system under test should be able to do. And keep a list of future tests you might want to write later.
Update both lists as you go, and do not hesitate to delete stuff from these lists when they are no longer relevant.
And if you still get stuck, undo everything you did since the last green test, take a short break, and go back to the drawing board.
CTA
How do you decide which test or code to write next? How often do you undo code and tests and go back to thinking? Tell me in the comments or on Twitter. I am @dtanzer there.
And if you liked this video, please subscribe and share it with your friends and followers using the share buttons below - That would be awesome!
Read / watch all parts of this series here:
- TDD: Red-Green Part 8: Conclusion
- TDD: Red-Green Part 7: Specific / Generic
- TDD: Red-Green Part 6: Triangulation
- TDD: Red-Green Part 5: Into a Corner
- TDD: Red-Green Part 4: List of Goals
- TDD: Red-Green Part 3: Why Red
- TDD: Red-Green Part 2: Wrong Code
- Red-Green Part 1: Introduction
- Red, Green, ... Part 0: All Posts