Adding New Behavior

When you think about it, the process of adding new behavior to a system isn’t so different from fixing a bug. The overall goal is to change some aspect of how the system behaves, without breaking anything else.

Just as with bug fixing, we can use characterization tests to pin down the surrounding behavior of the system to be sure it isn’t dislodged by our work. Characterization tests can also be useful before that, while the new feature is first being considered. You can use them to understand how much work is involved in the new feature by clarifying exactly what the system currently does.

Here’s our recipe for adding new behavior to a legacy system:

  1. Examine the new feature. If necessary, write a few characterization scenarios to examine and clarify the current behavior of the system in that area.

  2. Now, with the new feature in mind, modify those scenarios or write new ones to specify the desired new behavior.

  3. Run through the scenarios with your team’s stakeholder representative to check that you’re about to build the right thing. Correct the scenarios with them if necessary.

  4. Run the scenarios. If more than one fails, pick one, and examine the code you’ll need to change to make it pass.

  5. Write any extra characterization tests you need to give you the confidence to change that code.

  6. Change the code to make the scenario pass.

  7. Repeat from step 4 until all the scenarios pass.

Sometimes we find that when we’re about to implement the change (step 6), we see that the code we’re about to change is responsible for doing things that we hadn’t anticipated when we wrote the original characterization tests in step 1. At this point, we’ll stop and add some new characterization tests (step 5) first if we think there’s a significant risk we could break something.

Just as with bug fixing, following this process means that you quickly get the benefits of automated testing in the areas of the system where you most need them: the ones that are most prone to change and instability.

As you gradually build up your suite of Cucumber scenarios for your legacy application, you’ll find you have more confidence to refactor and clean up the code. This becomes a virtuous cycle, with cleaner code causing less bugs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.101.109