Published on

Using Gherkin's Background Syntax for Common Setup


Today one of the things I had to do was add integration tests to a process I was working on - we use Cucumber tests for this.

Cucumber is an awesome framework. I really like the way it promotes BDD where a BA can easily write up tests as part of their analysis in the form of:

     Given blah
     Then x happens
    ‎ Then y happens

It uses the Gherkin language to express test cases as in the above. What is great about this is that the basic syntax of Gherkin is very simple to understand. A well written Gherkin test written from a proper business perspective can easily be understood by devs (from any programming language), BAs, testers and business users.

One very useful part of Gherkin which a colleague and I used to share common setup across features is the Background syntax. You define a background in a feature file and then create step definitions for it as you would a Given, a When or a Thenin other features files. Backgrounds are almost the equivalent of your @Before JUnit methods where they are run before each scenario but after before. They are intended to have any setup you need run before each scenario. ‎