Getting Started with ATDD: Overcoming the Biggest Mistakes Right from the Start
If you're like me, you become amazed when you find out that common sense doesn't work as well as most people believe. Take weight loss, for example. Until recently I believed that I could count calories, track my weight, and that's all I needed to do to lose weight—well, maybe some exercising. Unfortunately, that plan didn't work for me. Since 2009, I've occasionally exercised up to six days per week, and I tried to eat less, but none of this effort showed up on my weight measurements.
After trying hard with the common-sense approach for more than three years, the situation changed when I found out that counting calories wasn't what I needed to do. Rather, I needed to change my diet. Measuring not only my weight but also the circumferences of my waist, upper arms, and upper legs helped me to lose 20 kilograms (about 44 pounds) in four months. All that I had needed was to read about a collection of things that worked for other people, and then decide which ones I wanted to try. Timothy Ferriss recorded such a collection in The 4-Hour Body [1] that I used as a basis, and I added some curious exploration.
The same lessons can be applied to acceptance test-driven development (ATDD). Teams usually start off wrong; they don't consider the right mixture of process, people, and tools. For most teams, the common approach to ATDD starts with selecting a tool, but getting started means considering a lot more before deciding on the tool. For some teams this rule may be surprising, but they're usually better off with postponing the tool decision until after more-pressing issues have been settled. Last year, a colleague approached me with a question: "We want to introduce acceptance tests and ATDD to a client. What tool should we start with?" It took me about an hour to introduce her to a better question: "Where do we start the approach?"
ATDD isn't about a tool like FitNesse, Cucumber, or Robot Framework. ATDD is about making sure that your development team develops the right product. A particular tool might help you in doing that, but it will also expose problems in your approach if you started on the wrong foot. Therefore, before starting with acceptance tests or ATDD, I prefer to introduce a working approach and then decide on the tool. The tool question needs to be answered at some point, but first we have to ask other demanding questions on our way to success.
Where Should I Start?
Okay, where should we actually start? In my practice, I start with an example. Working together, the team identifies a case that would derive the greatest benefit from being automated.
It's challenging to come up with a first approach. At this early point, the team doesn't know what to develop. We have to establish communication between the business stakeholders and the team. If we succeed with this plan, and trust in the team, we'll find out the details later.
Next we have to tackle the question of who should be included in the discussion. Ideally, I would include all team members, but sometimes doing so isn't practical or doesn't add value. Some forces involved in this decision need a closer look.
After settling these demanding questions, we choose a format. Notice that we're not yet down to the question of the tool, but we're starting to strive toward it. The format we choose will limit the choice of the tool, but the tool itself should be our last consideration. Instead of thinking about the tool, we should work for clarity in how we write down our examples.
The right approach first, the right people second, the right format third. Let's take a look into these separate ingredients.
The Right Approach
With ATDD, recognize that there is no "right" approach in general, but rather a right approach in the context of your team and your situation. For example, if you have an onsite customer as one of the core practices in extreme programming, you can probably rely on more informal specifications, because you can have discussions with your customer more often. If your customers and users are in a different building than the one housing your development team, you'll have to come up with a different approach.
Some teams start by holding specification workshops a week before their iteration starts. Some establish informal specifications with a group of three people, one or two days before the iteration planning. Others involve the end user. If your end user can free time only every other month, you might schedule a whole day and discuss more stories for the upcoming iterations.
Whatever your approach, leave enough room to come up with answers to puzzling questions. One of my teams had no easy access to their business users; sometimes they even seemed to contradict themselves. If you discuss acceptance criteria and examples during your iteration planning, you won't have a chance to avoid such contradictions. On the other hand, if you leave one or two days between discussing upcoming stories and planning, you'll benefit from just-in-time planning, as well as anticipating changes from users.
In any case, choose just enough time before the iteration planning to allow you to derive sufficient examples from complicated upcoming stories. For some teams, this might mean getting together the day before a one-week sprint; others might set aside a half day, one week before starting the next of their three-week iterations. Leave enough time to clarify upcoming questions for your customers and product owners.
The Right People
Who needs to be involved? Generally you should include people who can raise technical questions that are difficult to answer, and who can identify shortcomings and side-effects of the decisions you make. Minimally, this means including a programmer, a tester, and a representative from the business side. (George Dinwiddie calls this constellation "the Three Amigos." [2]) Or you might choose to gather your whole team. In any case, be sure to include at least one person from the three specialties. In a future article, we'll take a closer look at why these specification workshops outperform traditional requirements documents and specifications.
The Right Format
There are a lot of different ways to write down the examples available to you. I usually start with a simplistic verbal description, making sure to reflect the user goals in these examples. At this point, I try to avoid thinking about particular user-interface implementation details, which would cause my examples to be tied to the user interface later. I don't want to end up with a test maintenance problem if I have to revisit all of my examples just because the GUI changed to include a new button.
If I start seeing a pattern in my examples, I might decide to use a tabulated format. Tables usually express one common flow through the system in a repeated way. For example, consider the following table.
Numerator |
Denominator |
Quotient |
1 |
1 |
1 |
6 |
2 |
3 |
42 |
7 |
6 |
This table calculates the quotient of two given numbers for a variety of values. In any row of the table, the examples reflect the same operation: Given the numerator and denominator in the first two columns of the table, the quotient is expressed in the third column. The operation doesn't change at this point. You can express such a pattern in a less verbose way by using this sort of table structure. You can find the same concept as a parameterized test in xUnit frameworks, for example.
In most automated tests, I try to go for a structure that follows the three-phase test approach: We have some pre-conditions to settle, after which we have a single operation that triggers some behavior in the system under test, and finally we check any post-conditions that we can observe in the system. In the table above, the first two columns reflect the pre-conditions for our quotient test. The operation following the example is always the same—calculate the quotient of these two numbers. The expected results denoted in the third column will be asserted.
The BDD style Given/When/Then reflects the same approach to automation. Other names for this pattern include Arrange-Act-Assert and Setup-Execute-Verify. They all boil down to the same underlying concept.
Selecting a Tool
Sooner or later you'll face the decision of which tool to use. What's the right choice? After considering how you would like to work with ATDD, deciding who will be included in your discussions, and choosing the format that expresses your choice, find the tool that best suits those needs. At this point you've made all the difficult decisions. Maybe you even gained some experience by fostering communication first.
Your selected people might restrict the tool choice. For example, your product owner might not be comfortable with xUnit tests. On the other hand, if neither customers nor users will be looking closely at the examples once they're automated, maybe there's no need to choose a tool based on its user-friendliness for non-technical people. The long-term benefit of using the right tool definitely is based on who will actively use the tool. All the stakeholders of your decision should be able to live and work with the tool selection; that's why you need to include them right from the start in your decision-making.
If you decided in favor of a tabulated format, you may be limited to alternatives such as FitNesse, Robot Framework, or Concordion. Choosing a BDD-style format using Given/When/Then leaves your options more open. You can use one of the tools I just mentioned, or go for BDD-specific tools such as Cucumber, CucumberJVM, JBehave, or NBehave.
Some tools provide the ability to extract higher-level concepts from a set of given operations. These keywords (also known as scenario tables) come in handy if you want to provide non-technical testers with the ability to work alone on a set of predefined tests and still make progress. The development team prepares one or two examples with some variations. The keyword abilities of the tool give your testers a sort of "scripting" functionality, so that they can generate more tests based on the first prototype, without the need to develop much code in between.
One piece of advice: If you're considering an open source tool, make sure that it has an active user community. You don't want to start automating your examples and run into a problem with test number 13,372—only to find out that the product was abandoned five years ago by its users. You would have to evaluate a new tool anyway, but the transition might cost a lot of effort—despite your best intentions to keep your tests maintainable.
Getting Started with ATDD
All the successful teams I've worked with on ATDD shared one thing: They started with one approach, and they reflected on it regularly. During those retrospectives, they came up with necessary improvements if the approach stopped working for them. If you keep this practice in mind, you can essentially start with any approach. Strive for the approach that gets the least resistance from your team, and let them gather experience. As a coach, I make sure that my teams address concerns when they come up, and I enable them to make changes as needed. Only then are they free to adapt their process as indicated—even if I'm long gone.
The right approach, the inclusion of the right people, and the right format will drive your choice of a tool for ATDD, but you shouldn't avoid changing the tool if necessary. You might even start with record-and-playback tools, if you start to grow your own test library. Over time you'll build up more and more domain-specific tools for your application. These can easily be reused if you happen to change directions with your tool selection. Most teams that I've seen failing with ATDD stopped treating test automation as software development, and they gave up once the tools stopped serving them. Since they hadn't built up a reusable toolset, they couldn't easily change to a different tool. They abandoned ATDD altogether, just because they started on the wrong path.
Instead of failing like that, develop code that glues your application to the examples. By "develop," I mean "apply relentless refactoring," and I also drive the test automation code with TDD. Then I'll have a safety net that I can trust. I'll also be in an advantageous position if I have to make the difficult choice to replace my favorite tool—that change will be a lot less painful for me and my team.
References
[1] Timothy Ferriss, The 4-Hour Body: An Uncommon Guide to Rapid Fat-Loss, Incredible Sex, and Becoming Superhuman. Crown Archetype, 2010.
[2] George Dinwiddie, "The Three Amigos: All for One and One for All," StickyMinds.com, 11/29/2011.