Here is a quick tip if you want to create a step definition that has an argument with multiple lines. A multiline string argument if you like.
I wanted to test that an email was sent, with a specific subject, to a specific person, and containing a specific body text.
My idea was to create a step definition that looked something like this:
So basically my full file is now this:
My step definition looks like this:
Let’s try to run that.
Doing it that way simply does not work. You see, by default a line break in the Gherkin DSL has an actual meaning, so you can not do a line break in your argument, expecting it to just pass along everything up until the closing quote. What we actually want is to use a PyString. But how do we use them, and how do we define a step to receive them? Let’s start by converting our step definition to use the PyString multiline syntax:
Now let’s try to run it:
A bit closer. Our output actually tells us that we have a missing step definition, and suggests how to define it. That’s better. Let’s try the suggestion from the output, now defining our step like this:
The difference here is that we do not add the variable name for the body in the annotation, and we specify that we want a PyStringNode type parameter last. This way behat will know (tm).
After running the behat command again, we can finally use the step definition. Let's have a look at how we can use the PyString class.
And that about wraps it up. Writing tests are fun, right? As a bonus, here is an animated gif called "Testing".
Do you want to comment?
This article uses github for commenting. To comment, you can visit https://github.com/eiriksm/eiriksm.dev-comments/issues/3.