Skip certain steps in a scenario in Cucumber

2019-07-14 05:57发布

I have a cuke scenario,

Scenario: Hello World
Then do action one
Then do action two
Then do action three
Then do action four
Then do action five

But depending on the environment variable, I want to skip action three and action four. I know I can go in the step and do a if-else check, but it's not very elegant. Is there any better solution? Thanks :)

3条回答
迷人小祖宗
2楼-- · 2019-07-14 06:13

You can't do this in Gherkin, nor should you want to! Gherkin is not for programming, nor is it for stating 'how' things are done, instead its for stating 'what' things are and 'why' they are done.

In general when you find yourself wanting to program in Gherkin (loops, conditionals etc.) what you want to do is

  1. Write a more abstract scenario that removes the need for programming
  2. Push the programming down into the step definitions (or occasionally pull it up into a script that runs cucumber)

In your case you could do this by

When I act
Then ...

and

Given we are simple
When I act
Then ...

and use state set in the Given to allow conditional behaviour in the When

e.g.

Given "we are simple" do
  @simple = true
end

When "I act" do
  if @simple 
    simple_steps
  else
    all_steps
  end
end

Its much simpler and more productive to use Cucumber as its intended, rather than trying to make it do exactly what you want. Cucumber is not a general purpose testing tool, you're best either changing how you going to use it, or using a different tool. Good luck ...

查看更多
贪生不怕死
3楼-- · 2019-07-14 06:15

You can create two scenarios and use tags to filter them:

@complete-scenarios-tag
Scenario: Complete Hello World
    Then do action one
    Then do action two
    Then do action three
    Then do action four
    Then do action five

@simple-scenarios-tag
Scenario: Simple Hello World
    Then do action one
    Then do action two
    Then do action five

Then you can execute just the simple scenarios running:

cucumber --tags @simple-scenarios-tag
查看更多
Luminary・发光体
4楼-- · 2019-07-14 06:33

You can have code within the steps that recognizes the environment variables.

Given(/^the evironment has whatever$/) do
  if ENV['whatever'] == false
     do.something
  end
end

That's just an example. The code could do whatever you wanted.

查看更多
登录 后发表回答