Is it possible to write a gherkin step on multiple lines?

前提是你 提交于 2020-02-03 03:03:27

问题


I am new to the Gherkin language and this seems to me like very basic question but I could not find answer to it.

I am aware that it is possible to write multi-line step argument in Gherking, like this:

Given a blog post named "Random" with Markdown body
  """
  Some Title, Eh?
  ==============
  Here is the first paragraph of my blog post. Lorem ipsum dolor sit amet,
  consectetur adipiscing elit.
  """

My question is about writing single step on multiple lines, something like this:

Given that Gherkin language allows me to write my step definitions \
   on multiple lines
Then my test cases would be easier to read :)

In the example above I used '\' as line continuation symbol. BTW, I tried the example above and got parser error.


回答1:


You tried and got a parse error. I assume that writing the long line on a single line did not give you a parse error. You have therefore answered your own question with no.

I would like to suggest that you ask yourself why the lines has to be long. The wanted behaviour, does it really need so many details that the line gets very long?

It is often possible to express your self shorter by rephrasing a sentence. It is also possible that you could break the long sentence by using the Gherkin keywords And or But.




回答2:


Yes, we can write a gherkin step on multiple lines using triple set of double quotes ("""). Gherkin recognizes the triple set of double quotes as the bounding delimiters for the multi-line string and passes it in. Whatever content you write between triple set of double quotes will be passed to your step definition as a single string.

As in my current capybara project, I have written gherkin step on multiple lines as shown below:

Scenario: Some test scenario
  Given Bob is on "www.abc.com"
  When Bob creates team "Test Team"
  Then Bob sees message:
      """
      As the Team Captain you will be responsible for paying for the team after 
      registration closes. You will be emailed instructions at the close of 
      registration.
      """
And Bob clicks "Next" button

Step definition for multi line gherkins step:

And(/^(\S*) sees message:$/) do |user, message|
  page.should have_content(message)
end

In this I have used the content passed as it is. You can also split your content and use as required. For more information please refer to below mentioned link: http://asymmetrical-view.com/2011/06/02/cucumber-gherkin-and-multiline-arguments.html

Hope this helps :)



来源:https://stackoverflow.com/questions/35639150/is-it-possible-to-write-a-gherkin-step-on-multiple-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!