How do I conditionally skip a scenario?
我如何有条件地跳过场景?
For example, I wish to continue a scenario only if certain conditions are met, but I do not want it to register as a failure if it's not present.
例如,我希望仅在满足某些条件的情况下才继续使用方案,但如果不存在则我不希望将其注册为失败。
6 个解决方案
#1
6
This is an issue I had. The tests I write are against a UI that has a constantly changing BE database that I am currently unable to have static data in. This means that some times it is possible that there is no data for the test. Not a pass not a fail, just unable to run.
这是我遇到的一个问题。我编写的测试针对的是一个不断变化的BE数据库,我目前无法获取静态数据。这意味着有时可能没有测试数据。不是通过而不是失败,只是无法运行。
The way that I found to work best was to invoke a cucumber pending.
我发现最好的方法是调用黄瓜待定。
example test:
Scenario: Test the application
Given my application has data
When I test something
Then I get a result
example step def:
示例步骤def:
Given /^my application has data$/ do
pending unless application.has_data?
end
These are the kind of results I can see:
这些是我能看到的结果:
201 scenarios (15 pending, 186 passed)
1151 steps (15 pending, 1136 passed)
It's worth noting that I have extra debugging and have these tests tagged so that at any time I can run these pending tests again.
值得注意的是,我有额外的调试并且标记了这些测试,以便我可以在任何时候再次运行这些待处理的测试。
Hope this helps, Ben.
希望这有帮助,本。
#2
5
For anyone still looking for an answer to this: Apart from using pending
, or a specific profile to skip scenarios with certain tags, there are at least 2 more ways to achieve this.
对于仍在寻找答案的人:除了使用待处理或特定的配置文件跳过具有某些标签的方案之外,还有至少2种方法可以实现此目的。
I can understand why you would need this, as I had a similar problem and got a solution, hence worth sharing. In my case, I had a piece of functionality expected to be available on 3/10 devices, and expected to be not available on the remaining 7.
我可以理解为什么你需要这个,因为我有类似的问题并得到了解决方案,因此值得分享。在我的情况下,我有一个功能预计将在3/10设备上提供,并且预计在剩下的7个设备上不可用。
Caveats with using 'pending' to skip:
使用'待定'跳过的注意事项:
- Since the tests and code were implemented, it didn't feel right to mark steps as
pending
. - It caused confusion, as it was difficult to distinguish really
pending
scenarios from skipped but markedpending
scenarios at the end of a run - Some CI jobs (Jenkins/Hudson) might be configured to fail for pending scenarios, hence causing more trouble.
由于测试和代码已实施,因此将步骤标记为待处理是不对的。
它引起了混乱,因为在运行结束时很难区分真正未决的方案与跳过但标记为挂起的方案
某些CI作业(Jenkins / Hudson)可能会因未决方案而配置为失败,从而导致更多麻烦。
So, I rather wanted to just skip them during execution depending on the condition of which browser is being used. I also didn't want to have too many profiles specific to certain browsers/devices
所以,我宁愿在执行期间跳过它们,具体取决于使用哪种浏览器的条件。我也不希望有太多特定于某些浏览器/设备的配置文件
Solution:
- Use cucumber.yml to skip tagged scenarios conditionally
使用cucumber.yml有条件地跳过标记的场景
Here's a known ignored interesting fact about cucumber (from https://github.com/cucumber/cucumber/wiki/cucumber.yml):
这是一个关于黄瓜的已知忽略的有趣事实(来自https://github.com/cucumber/cucumber/wiki/cucumber.yml):
The cucumber.yml file is preprocessed by ERb; this allows you to use ruby code to generate values in the cucumber.yml file
cucumber.yml文件由ERb预处理;这允许您使用ruby代码在cucumber.yml文件中生成值
-
Building on this, tag your scenarios with something unique, say
@conditional
在此基础上,使用独特的东西标记您的场景,比如@conditional
-
At the beginning of your cucumber config (cucumber.yml), apply your conditional logic outside of any profiles mentioned:
在黄瓜配置(cucumber.yml)的开头,在所提到的任何配置文件之外应用条件逻辑:
<% included = (ENV['BROWSER'] =~ /chrome/) ? "-t @conditional" : "-t ~@conditional" %>
<%included =(ENV ['BROWSER'] =〜/ chrome /)? “-t @conditional”:“ - t~ @ conditional”%>
included
is just a variable, which will have a value of tags to include/exclude depending on the conditionincluded只是一个变量,根据条件,它将包含要包含/排除的标记值
-
Now use this conditional variable in the default profile
default: <%= included %>
现在在默认配置文件默认值中使用此条件变量:<%= included%>
-
So now your default profile will use the included/excluded tests as identified by your conditional logic.
因此,现在您的默认配置文件将使用条件逻辑标识的包含/排除测试。
- (More complicated and not elegant) Use rake tasks for cucumber execution:
(更复杂,更不优雅)使用rake任务执行黄瓜:
Conditionally choose tags to include/exclude within your rake task, and pass them to cucumber execution.
有条件地选择要在rake任务中包含/排除的标签,并将它们传递给黄瓜执行。
Hope this helps.
希望这可以帮助。
#3
3
You could check the condition before you start cucumber, then use a profile that would skip the scenarios with certain tags. Put this in your cucumber.yml:
您可以在开始黄瓜之前检查条件,然后使用可以跳过具有某些标签的方案的配置文件。把它放在你的cucumber.yml:
default: --tags ~@wip --tags ~@broken --no-source --color
limited: --tags @core --tags ~@wip --tags ~@broken --no-source --color
Replace @core with whatever tag you use for the cukes you want to run (or use ~ to exclude cukes). Then run the limited profile from a shell script that checks the conditions:
将@core替换为您要用于运行cukes的任何标记(或使用〜来排除cukes)。然后从检查条件的shell脚本运行有限的配置文件:
cucumber -p limited
#4
1
Short answer: Why?
简答:为什么?
Long answer: Cucumber is meant to be used to model the requirements of an end-user (typically customer). The features are meant to detail the list of scenarios in which they are expected to be used in, and the expected results that the features should exhibit when exposed to those scenarios. Those requirements are not conditional - the customer will want them to work all of the time. If Y should not work when X is disabled, then you put that in the Given clause, because that is a Scenario of Y.
答案很长:黄瓜用于模拟最终用户(通常是客户)的要求。这些功能旨在详细说明预期在其中使用的方案列表,以及功能在暴露于这些方案时应显示的预期结果。这些要求不是有条件的 - 客户希望他们一直工作。如果在禁用X时Y不起作用,则将其放在Given子句中,因为这是Y的场景。
Given sets up the state of your Scenario to something known. You are saying, "Given" these known variables, When I do this, Then this should happen. By 'conditionally skipping' a Cucumber clause, you are saying a feature should only work when another feature works.. in which case, why don't you put that in your given clause?
给定将场景的状态设置为已知的状态。你说,“给定”这些已知的变量,当我这样做时,那么这应该发生。通过'有条件地跳过'一个Cucumber条款,你说一个功能应该只在另一个功能有效时工作..在这种情况下,你为什么不把它放在你的给定条款中?
As far as I know, it is possible to skip steps (although I can't remember why), I'm merely questioning why you would want to do that. The only reason I can see you wanting to do this is to say that module A shouldn't work when module B is disabled. But your modules rely on each other then your code is coupled - which is exactly what Cucumber / TDD / BDD encourages you to avoid.
据我所知,有可能跳过步骤(虽然我不记得为什么),我只是质疑你为什么要这样做。我可以看到你想要这样做的唯一原因是,当模块B被禁用时,模块A不应该工作。但是你的模块相互依赖,然后你的代码被耦合 - 这正是Cucumber / TDD / BDD鼓励你避免的。
Example I posted in the comments:
我在评论中发布的示例:
Feature: Slicing Bread
As a hungry person
I want to be able to slice bread
so that I can eat smaller bits of my sandwich and enjoy it much more
Scenario: Slicing bread in half
Given a knife and a piece of bread
When I cut the bread in half
Then I should have two pieces of bread
This scenario would not hold up if the 'Given' requirements failed. If the 'Given' requirements failed - for example, Given a spoon and some soup
, you can't possibly expect to end up with two pieces of bread at the end of it. Therefore you are trying to test undefined behaviour. Are you trying to say that if you have a spoon and some soup that cutting bread should no longer work in this universe? No? Then why skip steps?
如果'Given'要求失败,这种情况就不会成功。如果'给定'要求失败 - 例如,给一把勺子和一些汤,你不可能期望最后得到两片面包。因此,您正在尝试测试未定义的行为。你是想说,如果你有一把勺子和一些汤,切割面包应该不再在这个宇宙中工作?没有?那为什么要跳过步骤?
#5
1
Please see this solution which truly skips the scenario instead of trowing a pending error:
请参阅此解决方案,该解决方案真正跳过了方案,而不是抛出待处理的错误:
Before do |scenario|
scenario.skip_invoke!
end
#6
1
I am tagging my scenarios, and then in my "step_definitions/hooks.rb" file, I have something like this:
我正在标记我的场景,然后在我的“step_definitions / hooks.rb”文件中,我有这样的事情:
Before('@proxy') do
skip_this_scenario unless proxy_running?
end
scenario.skip_invoke!
which was mentioned in another answer seems to be deprecated.
scenario.skip_invoke!在另一个答案中提到的似乎已被弃用。
#1
6
This is an issue I had. The tests I write are against a UI that has a constantly changing BE database that I am currently unable to have static data in. This means that some times it is possible that there is no data for the test. Not a pass not a fail, just unable to run.
这是我遇到的一个问题。我编写的测试针对的是一个不断变化的BE数据库,我目前无法获取静态数据。这意味着有时可能没有测试数据。不是通过而不是失败,只是无法运行。
The way that I found to work best was to invoke a cucumber pending.
我发现最好的方法是调用黄瓜待定。
example test:
Scenario: Test the application
Given my application has data
When I test something
Then I get a result
example step def:
示例步骤def:
Given /^my application has data$/ do
pending unless application.has_data?
end
These are the kind of results I can see:
这些是我能看到的结果:
201 scenarios (15 pending, 186 passed)
1151 steps (15 pending, 1136 passed)
It's worth noting that I have extra debugging and have these tests tagged so that at any time I can run these pending tests again.
值得注意的是,我有额外的调试并且标记了这些测试,以便我可以在任何时候再次运行这些待处理的测试。
Hope this helps, Ben.
希望这有帮助,本。
#2
5
For anyone still looking for an answer to this: Apart from using pending
, or a specific profile to skip scenarios with certain tags, there are at least 2 more ways to achieve this.
对于仍在寻找答案的人:除了使用待处理或特定的配置文件跳过具有某些标签的方案之外,还有至少2种方法可以实现此目的。
I can understand why you would need this, as I had a similar problem and got a solution, hence worth sharing. In my case, I had a piece of functionality expected to be available on 3/10 devices, and expected to be not available on the remaining 7.
我可以理解为什么你需要这个,因为我有类似的问题并得到了解决方案,因此值得分享。在我的情况下,我有一个功能预计将在3/10设备上提供,并且预计在剩下的7个设备上不可用。
Caveats with using 'pending' to skip:
使用'待定'跳过的注意事项:
- Since the tests and code were implemented, it didn't feel right to mark steps as
pending
. - It caused confusion, as it was difficult to distinguish really
pending
scenarios from skipped but markedpending
scenarios at the end of a run - Some CI jobs (Jenkins/Hudson) might be configured to fail for pending scenarios, hence causing more trouble.
由于测试和代码已实施,因此将步骤标记为待处理是不对的。
它引起了混乱,因为在运行结束时很难区分真正未决的方案与跳过但标记为挂起的方案
某些CI作业(Jenkins / Hudson)可能会因未决方案而配置为失败,从而导致更多麻烦。
So, I rather wanted to just skip them during execution depending on the condition of which browser is being used. I also didn't want to have too many profiles specific to certain browsers/devices
所以,我宁愿在执行期间跳过它们,具体取决于使用哪种浏览器的条件。我也不希望有太多特定于某些浏览器/设备的配置文件
Solution:
- Use cucumber.yml to skip tagged scenarios conditionally
使用cucumber.yml有条件地跳过标记的场景
Here's a known ignored interesting fact about cucumber (from https://github.com/cucumber/cucumber/wiki/cucumber.yml):
这是一个关于黄瓜的已知忽略的有趣事实(来自https://github.com/cucumber/cucumber/wiki/cucumber.yml):
The cucumber.yml file is preprocessed by ERb; this allows you to use ruby code to generate values in the cucumber.yml file
cucumber.yml文件由ERb预处理;这允许您使用ruby代码在cucumber.yml文件中生成值
-
Building on this, tag your scenarios with something unique, say
@conditional
在此基础上,使用独特的东西标记您的场景,比如@conditional
-
At the beginning of your cucumber config (cucumber.yml), apply your conditional logic outside of any profiles mentioned:
在黄瓜配置(cucumber.yml)的开头,在所提到的任何配置文件之外应用条件逻辑:
<% included = (ENV['BROWSER'] =~ /chrome/) ? "-t @conditional" : "-t ~@conditional" %>
<%included =(ENV ['BROWSER'] =〜/ chrome /)? “-t @conditional”:“ - t~ @ conditional”%>
included
is just a variable, which will have a value of tags to include/exclude depending on the conditionincluded只是一个变量,根据条件,它将包含要包含/排除的标记值
-
Now use this conditional variable in the default profile
default: <%= included %>
现在在默认配置文件默认值中使用此条件变量:<%= included%>
-
So now your default profile will use the included/excluded tests as identified by your conditional logic.
因此,现在您的默认配置文件将使用条件逻辑标识的包含/排除测试。
- (More complicated and not elegant) Use rake tasks for cucumber execution:
(更复杂,更不优雅)使用rake任务执行黄瓜:
Conditionally choose tags to include/exclude within your rake task, and pass them to cucumber execution.
有条件地选择要在rake任务中包含/排除的标签,并将它们传递给黄瓜执行。
Hope this helps.
希望这可以帮助。
#3
3
You could check the condition before you start cucumber, then use a profile that would skip the scenarios with certain tags. Put this in your cucumber.yml:
您可以在开始黄瓜之前检查条件,然后使用可以跳过具有某些标签的方案的配置文件。把它放在你的cucumber.yml:
default: --tags ~@wip --tags ~@broken --no-source --color
limited: --tags @core --tags ~@wip --tags ~@broken --no-source --color
Replace @core with whatever tag you use for the cukes you want to run (or use ~ to exclude cukes). Then run the limited profile from a shell script that checks the conditions:
将@core替换为您要用于运行cukes的任何标记(或使用〜来排除cukes)。然后从检查条件的shell脚本运行有限的配置文件:
cucumber -p limited
#4
1
Short answer: Why?
简答:为什么?
Long answer: Cucumber is meant to be used to model the requirements of an end-user (typically customer). The features are meant to detail the list of scenarios in which they are expected to be used in, and the expected results that the features should exhibit when exposed to those scenarios. Those requirements are not conditional - the customer will want them to work all of the time. If Y should not work when X is disabled, then you put that in the Given clause, because that is a Scenario of Y.
答案很长:黄瓜用于模拟最终用户(通常是客户)的要求。这些功能旨在详细说明预期在其中使用的方案列表,以及功能在暴露于这些方案时应显示的预期结果。这些要求不是有条件的 - 客户希望他们一直工作。如果在禁用X时Y不起作用,则将其放在Given子句中,因为这是Y的场景。
Given sets up the state of your Scenario to something known. You are saying, "Given" these known variables, When I do this, Then this should happen. By 'conditionally skipping' a Cucumber clause, you are saying a feature should only work when another feature works.. in which case, why don't you put that in your given clause?
给定将场景的状态设置为已知的状态。你说,“给定”这些已知的变量,当我这样做时,那么这应该发生。通过'有条件地跳过'一个Cucumber条款,你说一个功能应该只在另一个功能有效时工作..在这种情况下,你为什么不把它放在你的给定条款中?
As far as I know, it is possible to skip steps (although I can't remember why), I'm merely questioning why you would want to do that. The only reason I can see you wanting to do this is to say that module A shouldn't work when module B is disabled. But your modules rely on each other then your code is coupled - which is exactly what Cucumber / TDD / BDD encourages you to avoid.
据我所知,有可能跳过步骤(虽然我不记得为什么),我只是质疑你为什么要这样做。我可以看到你想要这样做的唯一原因是,当模块B被禁用时,模块A不应该工作。但是你的模块相互依赖,然后你的代码被耦合 - 这正是Cucumber / TDD / BDD鼓励你避免的。
Example I posted in the comments:
我在评论中发布的示例:
Feature: Slicing Bread
As a hungry person
I want to be able to slice bread
so that I can eat smaller bits of my sandwich and enjoy it much more
Scenario: Slicing bread in half
Given a knife and a piece of bread
When I cut the bread in half
Then I should have two pieces of bread
This scenario would not hold up if the 'Given' requirements failed. If the 'Given' requirements failed - for example, Given a spoon and some soup
, you can't possibly expect to end up with two pieces of bread at the end of it. Therefore you are trying to test undefined behaviour. Are you trying to say that if you have a spoon and some soup that cutting bread should no longer work in this universe? No? Then why skip steps?
如果'Given'要求失败,这种情况就不会成功。如果'给定'要求失败 - 例如,给一把勺子和一些汤,你不可能期望最后得到两片面包。因此,您正在尝试测试未定义的行为。你是想说,如果你有一把勺子和一些汤,切割面包应该不再在这个宇宙中工作?没有?那为什么要跳过步骤?
#5
1
Please see this solution which truly skips the scenario instead of trowing a pending error:
请参阅此解决方案,该解决方案真正跳过了方案,而不是抛出待处理的错误:
Before do |scenario|
scenario.skip_invoke!
end
#6
1
I am tagging my scenarios, and then in my "step_definitions/hooks.rb" file, I have something like this:
我正在标记我的场景,然后在我的“step_definitions / hooks.rb”文件中,我有这样的事情:
Before('@proxy') do
skip_this_scenario unless proxy_running?
end
scenario.skip_invoke!
which was mentioned in another answer seems to be deprecated.
scenario.skip_invoke!在另一个答案中提到的似乎已被弃用。