Gherkin 场景应该总是有When步骤吗? [英] Should Gherkin scenario always have When step?

查看:41
本文介绍了Gherkin 场景应该总是有When步骤吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Gherkin 中定义场景时,有时 Given 和 When 步骤之间没有明确的区别,即用户没有与系统进行主动交互,验证的目的是验证系统在某些情况下的外观.

When defining scenarios in Gherkin sometimes there is no clear distinction between Given and When steps, i.e. there is no active interaction with the system from the user and the purpose of the validation is to verify how the system should look under certain circumstances.

考虑以下几点:

Scenario: Show current balance
Given user is on account page
Then user should see his balance

Scenario: Show current balance
When user goes to account page
Then user should see his balance

我不确定我是否会一直使用第二种变体.如果我有多个场景共享上下文用户在帐户页面"并且其中一些具有其他用户操作而其他人没有,那么在我看来,将用户在帐户页面"作为给定步骤保持应该是有效的即使在某些情况下它可能缺少何时".这是一种有效的方法吗?

I am not sure I would always use the second variant. If I have multiple scenarios sharing the context "user is on account page" and some of them have additional user actions while others don't, then it seems to me it should be valid to keep "user in account page" as a Given step even though it may lack "When" for some scenarios. Is this a valid approach?

推荐答案

从形式上和技术上 Cucumber/SpecFlow 不需要你写一个When-step,或者更确切地说 Given/When/Then 只是按照它们编写的顺序执行在场景中.在这方面,您不需要When-step.

Formally and technically Cucumber/SpecFlow doesn't require you to write a When-step or rather Given/When/Then's are just executed in the order they are written in the scenario. In that regard you don't need a When-step.

但是,正如 Andy Waite 所写,When 步骤显示了您的系统从设置"采取的操作或事件,以达到您在 Then 步骤中验证的新状态.在这方面,每个测试中都应该出现一个When-step(正如您所写的那样:我们要测试什么).

But, as Andy Waite wrote about, the When-step shows on the action or event that your system takes from the "Setup" to get to the new state that you verifies in the Then-step. In that regard a When-step should be present in every test (as you wrote: what are we testing otherwise).

留下您的最终评论;仅验证设置怎么样(假设系统已启动,那么数据库是干净的,作为一个幼稚的例子).在这种情况下,可以跳过何时步骤.

That leaves your final comment; what about verifying just the setup (Given the system is started, Then the database is clean as a naïve example). In such scenarios the When-step could be skipped.

因此,一如既往,它归结为可读性和理解力.编写场景是为了使我们对系统行为的想法具体而清晰.使用优化的形式来理解和学习相关行为.

So, as always, it comes down to readability and understanding. Scenarios are written to make our thoughts about the systems behavior concrete and clear. Use the form that optimize for understanding and learning about the behavior in question.

如果不考虑太多,我可能会猜测一般建议是始终使用使事件或行为非常明显和清晰的When-step.如果可能,我会回避隐含和隐藏的行为.

Without thinking too hard on this I would probably guess that the general advice is to always use a When-step that makes the event or behavior very apparent and clear. I would shy away from implicit and hidden behavior when possible.

我希望这会有所帮助.

这篇关于Gherkin 场景应该总是有When步骤吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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