The Cucumber Book (2nd Edition) Summary - Part 1 - Fundamentals
tl;dr: Read the book, it’s that good !
Recently I started to read the book, which was written by the creator of Cucumber themselves. The book walks the reader through Cucumber features, and some guidance on how to effectively use Cucumber.
It also has examples on how to use Cucumber in different types of applications - CLI, REST Web Service, Ruby on Rails, and AJAX Web applications. The book also helps to identify situations on how to spot when Cucumbers go bad, which I will cover in Part 2 of the summary. In Part 1, I will focus on Cucumber fundamentals.
Here are a few TILs from the book:
-
Step Definition
will match anything no matter if it is defined asGiven()
,When()
, orThen()
. For example, a step defined asGiven()
, will also match a a step called from aScenario
asWhen
orThen
, as long as the regular expression matches. TheGiven()
,When()
, andThen()
are just aliases to make things more readable and organisable. That also explains whyAnd
andBut
can be used to match anyStep Definition
without the need to implementAnd()
orBut()
. -
Related to the preceding item, it is possible to replace
Given
,When
,Then
,But
andAnd
inFeature
file with asterisks*
. However the readability is debatable and it depends on the team whether it makes sense to the more verbose standard keywords or asterisks:
-
All steps will be treated as passed by default, unless an exception is returned.
-
Feature
keyword is just for documentation purposes. It can support multine description, or even empty. -
Data Table
can be used to describe information not fit on a single line of step. So no more multiple steps to describe a complexScenario
.Data Table
can be accessed using an array-like structure within the steps. Example:
Scenario Outline
supports multipleExamples
tables, each with own description. This feature enables categorising of examples, like Positive case, Exceptional case, etc. and at the same time sharing the sameScenario Outline
. Example:
-
Nested Steps
are allowed in Cucumber. That means aStep Definition
can call anotherStep Definition
. However it is discouraged, as most of the time, it is easier to implement certain logic in theStep Definition
itself instead of abstracting it further throughNested Steps
. Reference -
Doc Strings
allow multiline data to be attached to a preceding step. This allows some snippets like JSON, XML, HTML, etc. to be specified and attached inline in aScenario
. Example:
-
Tags
can be used atFeature
,Scenario
,Scenario Outline
andExamples
levels. Tags can be inherited if they are defined at higher levels. Multiple tags can be specified using spaces (@slow @endtoend
). Tags are useful because they can be used for documentation, filtering for running or reporting, and also for before/afterhooks
that can target specific tags. -
Background
should be used to set up complex conditions, so that theScenario
only contain its essence and intent, without the noise. -
Split
Scenario
if it is too long. Likewise, splitFeature
if it is too complex. -
Scenario
name should focus more on theGiven
andWhen
, not so much onThen
-
Beware of
Step
phrasing, as one ambiguous step intended forputting the data
,checking the data
, ormodifying the data
might be mistakenly reused to compose another Scenario that may result in invalid outcome. For example, a step calledGiven the account balance is $50
could be implemented to set up the account balance, however someone could mistekenly reuse it to check the account balance in aThen
step:Then the account balance is $50
. Then the result will be wrong as the step will put the data instead of checking the data, and the step will always pass.
I think that’s it for Part 1. Part 2 will cover how to spot when Cucumbers go bad, the symptoms, the causes and the remedy.