Asymmetrical View

Cucumber, Gherkin and Multi-line arguments

Gherkin is the language of the Cucumber testing tool uses for describing feature tests.

Writing a test today at $WORK, we needed to use a multi-line string and had some trouble finding an example in Google…

I am using Cucumber to test my Twilio based IVR and SMS applications. You can find an example as part of my Twilio in Ten Minutes repo, one of which I’ve included here:

The Scenario “I register my card in one step and sign up for all services.” shows how you embed a multi-line string into Gherkin – you surround your text with enclosing sets of triple double quotes.

I implemented two steps in my testing framework: the first supports a double quoted, single-line string. The second supports the multi-line string:

Note that the matcher for the multi-line string doesn’t include a capture group. The multi-line string will end up being passed in anyway, as Gherkin recognizes the triple set of double quotes as the bounding delimiters for the multi-line string and passes it in.

The multi-line string will be passed to your step definition as a single string, which you can use as-is or split apart as I’m doing in my step definition. Note that it will include any whitespace indentation present in your feature definition.

This will now make for an easy reminder for myself and hopefully it’ll show up the next time someone else searches Google for how to use multiline strings.

Kyle Burton, 06 Jun 2011 – Philadelphia PA

Tags: testing,bdd,cucumber,gherkin