Ruby 在Cucumber中跳过场景中的某些步骤

Ruby 在Cucumber中跳过场景中的某些步骤,ruby,testing,cucumber,Ruby,Testing,Cucumber,我有一个想吐的场景 Scenario: Hello World Then do action one Then do action two Then do action three Then do action four Then do action five 但是根据环境变量的不同,我想跳过操作三和操作四。我知道我可以在步骤中进行if-else检查,但这不是很优雅。有没有更好的解决办法?谢谢:您可以创建两个场景并使用来筛选它们: @complete-scenarios-tag Scenari

我有一个想吐的场景

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

但是根据环境变量的不同,我想跳过操作三和操作四。我知道我可以在步骤中进行if-else检查,但这不是很优雅。有没有更好的解决办法?谢谢:

您可以创建两个场景并使用来筛选它们:

@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
然后,您可以只执行运行的简单场景:

cucumber-tags@simple-scenarios-tag
您可以在步骤中包含识别环境变量的代码

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

这只是一个例子。代码可以做你想做的任何事情。

你不能在小黄瓜中做这件事,你也不应该这样做!小黄瓜不是用来编程的,也不是用来说明事情是如何做的,而是用来说明事情是什么以及为什么要做

通常,当您发现自己想要在小黄瓜循环、条件等中编程时,您想要做的是

编写一个更抽象的场景,消除编程的需要 将编程向下推到步骤定义中,或者偶尔将其拉到运行的脚本中 在你的情况下,你可以通过

When I act
Then ...

并使用给定中的状态集来允许在

e、 g


它更简单,更有效地使用黄瓜作为它的意图,而不是试图让它做你想要的。Cucumber不是一个通用测试工具,您最好改变使用它的方式,或者使用不同的工具。祝你好运…

谢谢你的建议,但我不想复制和维护两组场景。我真正想要的是,根据环境变量,运行某些步骤,跳过某些步骤。我认为在小黄瓜中没有if-then-else。那么你就不能使用.feature文件跳过一个步骤。这正是我目前正在做的。但最理想的方法是,我可以在功能文件本身中指出我想要跳过的内容,因此很容易发现。只是在这个建议的基础上,我必须将我的主代码块放在if子句中,并且我不能一步返回或中断。Cucumber不是设计用来这样做的。
Given we are simple
When I act
Then ...
Given "we are simple" do
  @simple = true
end

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