How to skip Cucumber steps?

ぃ、小莉子 提交于 2020-01-01 10:10:13

问题


I have the scenario like:

  1. Given I go to this page
  2. When I type cucumber
  3. And I click
  4. Then I should see the text
  5. And I should not see the line

If I run this scenario it will execute all the 5 steps. But I want to skip the 4th step (Then I should see the text) and execute the 5th step.

Please give me your suggestions. Thanks in advance. :)


回答1:


TL;DR - don't do it - you're (probably) getting it wrong. And you can't (easily) do it. As Aslak wrote (one of Cucumber main creators):

A step can have the following results:

  • undefined (no matching stepdef)
  • pending (a matching stepdef that throws PendingException)
  • passed (a matching stepdef that doesn't throw anything)
  • failed (a matching stepdef that throws an exception that isn't PendingException)
  • skipped (a step following a step that threw any exception (undefined, pending or failed))

What you're asking for is a new kind of result - ignored. It could be implemented by throwing an IgnoredException. This would mean that skipped would have to be changed to: (a step following a step that threw any exception (undefined, pending or failed) - unless the exception was IgnoredException)

I'm not sure I like this. It sounds like more complicated than it needs to be. You already have the necessary information that something is amiss - your step will either fail or be pending. I don't see the value in continuing to execute the following steps. You still have to implement the failing/pending step.

As long as you're reminded that "there is work to be done here" I don't think it's wortwhile complicating Cucumber to tell you what kind of work needs to be done...

Aslak

Whole discussion is here: http://comments.gmane.org/gmane.comp.programming.tools.cucumber/10146




回答2:


I have had to skip steps conditionally based on environments. I used next to skip steps. Here is an example on how to do what you wanted.

Then /^I should see the text/$ do
    next if @environment == 'no text'

    ...
       <The actual step definition>
    ...
end


来源:https://stackoverflow.com/questions/13988125/how-to-skip-cucumber-steps

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