Writing automated tests that help you drive your development is a powerful tool that every software developer should try to acquire. The biggest problem is the learning curve, how do you go from just knowing about it to being able to do it all the time? This may seem like an enormous task, and at face value it is, but you can start the ball rolling quite easily.
Many, including myself, try to start implementing test driven development directly in their project. This is a very noble effort, but in the majority of cases it fails and leaves the team with the understanding that the cost of testing is too high and not worth doing.
There is another way.
Be Up Front
- Becoming good at writing test code is hard.
- Writing good test code is different than writing good production code.
- Writing tests will change the way you write production code.
- Writing tests for code that were not designed to be testable is the hardest kind of tests to write.
Since tests will change the production code and untestable code is the hardest to start testing, your day-to-day project is probably not the best place to get your feet wet. What you are looking for is practice space, an environment where you can extend your skills without deadlines pressuring you back to your comfortable ways of developing.
Create a Practice Space
To create the simplest practice space you only need a few things.
- A very basic understanding of the TDD steps.
- A development environment with a testing tool, Eclipse and JUnit are an easy start in the java world.
- At least 1 hour of time to dedicate.
- A peer to pair program with, you learn faster when you collaborate with a peer.
- A problem to work on.
- Iterate!
Find the amount of time to dedicate that you can continually do every week. The constant practice is what will allow this new skill to stick and eventually turn into a habit.
Having another person pair with you, means you are less likely to get stuck. If both of you get stuck all the time, the problem you are currently working may be a bit above your current skill level, select a simpler one and try again.
What kinds of problems should you use to start your journey into the world of TDD? Some good ones to start out with are what are known as coding katas. A few I would suggest are:
- Backwards Talk
- FizzBuzz
- Bowling Game
- Prime Factors
- Shunting Yard Algorithm
- and many more at codingkata.org and codingdojo.org
Practice Makes Perfect
This will give you a great starting point. After practicing for a while you will start seeing opportunities where you can easily start using tests to drive your day-to-day development. You need to practice to become good. Concert pianists don’t just start performing in front of a paying audience.