Blog

Then When Given

17 Oct, 2014
Xebia Background Header Wave

People who practice ATDD all know how frustrating it can be to write automated examples. Especially when you get stuck overthinking the preconditions of examples.

This post describes an alternative approach to writing acceptance tests: write them backwards!

Imagine that you are building the very first online phone book. We need to define an acceptance tests for viewing the location of a florist. Using the Given-When-Then formula you would probably describe the behaviour like this.

<br /> Given I am on the online phone book homepage<br /> When I type “Florist” in the business type field<br /> And I click …<br /> ...<br />

Most of the time you will be discussing and describing details that have nothing to do with viewing the location of a florist. To avoid this, write down the Then clause of the formula first.
Make sure the Then clause contains an observable result.

<br /> Then I see the location “Floriststreet 123”<br />

Next, we will try to answer the following question: What caused the Then clause?
Make sure the When clause contains an actor and an action.

<br /> When I click “View map” of the search result<br /> Then I see the location “Floriststreet 123”<br />

The last thing we will need to do is answer the following question: Why can I perform that action?
Make sure the Given clause contains a simple precondition.

<br /> Given I see a search result for florist “Floral Designs”<br /> When I click “View map” of the search result<br /> Then I see the location “Floriststreet 123”<br />

You might have noticed that I left out certain parts where the user goes to the homepage and selects UI objects in the search area. It was not worth mentioning in the Given-When-Then formula. Too much details make us lose focus of what we really want to check. The essence of this acceptance test is clicking on the link “View map” and exposing the location to the user.

Try it a couple of times and let me know how it went.

Qxperts. We empower companies to deliver reliable & high-quality software. Any questions? We are here to help! www.qxperts.io

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts