Writing Acceptance Tests

You now start to convert the requirements you identified into acceptance tests. At this stage you’re actually using the Gherkin language, which consists of fewer than a dozen key words and a few pieces of punctuation. The best (and easiest to read) documentation for the Gherkin language is at the Behat project, so for more detail, please refer to http://docs.behat.org/en/gherkin/index.html.

Given, When, and Then[24] are Gherkin keywords, but for the purposes of writing the acceptance test, they are used to describe the context in which an event will happen, and then describe the expected outcome.

Now, at this point, it’s necessary to understand the connection between the Cucumber feature and the other two aspects of Cucumber—the command itself, and the step definitions.

Step definitions are methods written in a high-level programming language which set up the scenarios, and perform various tests to determine whether the resulting state matches the intended state. At the time of writing, there are at least eight supported languages for writing step definitions. We’re going to be using Ruby. The steps in the scenario map directly to the step definitions. Here’s a trivial example:

Given an adding machine
When I input the numbers "1" "2" "3"
Then the answer should be "6"

The matching step definitions might say:

Given /^an adding machine$/ do machine = AddingMachine.new end When /^I input the numbers "([^"]*)" "([^"]*)" "([^"]*)"$/ do |arg1, arg2, arg3| answer = machine.add(arg1, ...

Get Test-Driven Infrastructure with Chef now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.