在黄瓜中的场景中跳过某些步骤 [英] Skip certain steps in a scenario in Cucumber

查看:891
本文介绍了在黄瓜中的场景中跳过某些步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库克场景,

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检查,但这不是很优雅。有更好的解决方案吗?谢谢:)

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 :)

推荐答案

您无法在Gherkin中做到这一点,也不应该这样做!小黄瓜既不是为了编程,也不是为了说如何做事,而是为了说明什么事以及为什么要做。

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.

通常,当您发现自己想要在Gherkin中编程(循环,条件语句等)时,您要做的就是

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


  1. 写一个更抽象的无需编程的场景

  2. 将编程推入步骤定义中(或偶尔将其拉入运行黄瓜的脚本中)

在您的情况下,您可以这样做

In your case you could do this by

When I act
Then ...

Given we are simple
When I act
Then ...

并使用给定中设置的状态以允许在何时

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

例如

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

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

使用Cucumber达到预期目的要简单得多,而且效率更高,而不是试图使其完全按照您的要求进行操作。 Cucumber不是通用测试工具,最好更改使用方式或使用其他工具。祝你好运...

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 ...

这篇关于在黄瓜中的场景中跳过某些步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆