问题
I have the scenario like:
- Given I go to this page
- When I type cucumber
- And I click
- Then I should see the text
- 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