在 Gherkin 中分组步骤或连接场景
Grouping steps or concatenating scenarios in Gherkin
我正在使用 Behat or Cucumber, using the Gherkin 语言等工具定义要在 BDD 工作流程中使用的功能。这是到目前为止的特征定义:
Feature: Save Resource
In order to keep interesting resources that I want to view later
As a user
I need to be able to save new resources
Scenario: Saving a new resource
Given "http://google.com" is a valid link
When I insert "http://google.com" as the new resource link
And I insert "Google Search Engine" as the new resource title
Then I should see a confirmation message with the inserted link and title
When I accept
Then I should have 1 additional resource with the inserted link and title
Scenario: Aborting saving a new resource
Given "http://google.com" is a valid link
When I insert "http://google.com" as the new resource link
And I insert anything as the new resource title
Then I should see a confirmation message with the inserted link and title
When I abort
Then I should have the same number of resources as before
Scenario: Saving a resource with invalid link
Given "http://invalid.link" is an invalid link
When I insert "http://invalid.link" as the new resource link
And I insert anything as the new resource title
Then I should see a confirmation message with the inserted link and title
When I accept
Then I should see an error message telling me that the inserted link is invalid
Scenario: Saving a resource with already saved link
Given "http://google.com" is an already saved link
When I insert "http://google.com" as the new resource link
And I insert anything as the new resource title
Then I should see a confirmation message with the inserted link and title
When I accept
Then I should see an error message telling me that the inserted link already exists
如您所见,在多个场景中重复了很多样板文件。我知道我可以定义一个 Background
,其中包含在所有场景之前要执行的步骤列表,我可以将所有步骤放在确认消息之前,但是如果我这样做,我将无法区分在用户可能插入的不同链接和标题中。
是否可以定义一个仅用于某些场景而不是所有场景的背景?或者可能连接两个场景,例如要求某个场景(我可以重复使用)在另一个场景之前是 运行?还是我应该继续重复样板文件?
我会考虑考虑你的场景应该做什么。目前,我看到的脚本讨论了如何完成工作。几乎没有什么应该做的。
导航详细信息对于理解系统应该做什么并不是很有用。这是一个众所周知的初学者错误,如 [1] 中所述并转录于 [2]。
您要做的是将 UI 详细信息压入堆栈。导航详细信息在您实施的步骤使用的助手 methods/classes 中生活得更好。页面对象是从场景中隐藏导航的一种方式。
当您去掉导航细节时,您的一些重复项将会消失。出于可读性原因,剩余的重复可能是可以接受的。
请记住,理解场景比一点点重复重要得多。
[1] https://cucumber.io/blog/2016/05/09/cucumber-antipatterns
[2] http://www.thinkcode.se/blog/2016/06/22/cucumber-antipatterns
继 Thomas 的回答后
您的方案复杂且重复,因为每次它们描述 'how' 用户都与应用程序交互。 'How' 在场景中没有位置,因为
随着您对自己正在做的事情了解得更多,您做事方式的细节可能会发生变化。您不希望每次更改操作细节时都必须更改场景
在您的场景中添加内容会使它们变得无聊、重复、难以阅读且实施起来成本高昂。
描述你的场景通常是一种避免做场景的实际工作的方法,即找出'why'你正在做的事情并优雅和简洁地定义'what'您正在努力实现。
n.b。还有很多其他原因
让我们看看当您完成这些额外的工作后,您的场景会变得多么简单
##旧
Scenario: Saving a new resource
Given "http://google.com" is a valid link
When I insert "http://google.com" as the new resource link
And I insert "Google Search Engine" as the new resource title
Then I should see a confirmation message with the inserted link and title
When I accept
Then I should have 1 additional resource with the inserted link and title
##新
Scenario: Bookmarking
When I save a bookmark
Then my bookmark should be saved
##旧
Scenario: Saving a resource with invalid link
Given "http://invalid.link" is an invalid link
When I insert "http://invalid.link" as the new resource link
And I insert anything as the new resource title
Then I should see a confirmation message with the inserted link and title
When I accept
Then I should see an error message telling me that the inserted link is invalid
##新
Scenario: Bookmark with invalid link
When I bookmark with an invalid link
Then I should see an invalid link error
等等
请注意新场景如何清楚地说明您在增加业务价值方面所做的工作有多么少!!
关于您关于背景的问题,如果您需要两个不同的背景,您需要两个不同的 .feature 文件。这是好事!!
我正在使用 Behat or Cucumber, using the Gherkin 语言等工具定义要在 BDD 工作流程中使用的功能。这是到目前为止的特征定义:
Feature: Save Resource
In order to keep interesting resources that I want to view later
As a user
I need to be able to save new resources
Scenario: Saving a new resource
Given "http://google.com" is a valid link
When I insert "http://google.com" as the new resource link
And I insert "Google Search Engine" as the new resource title
Then I should see a confirmation message with the inserted link and title
When I accept
Then I should have 1 additional resource with the inserted link and title
Scenario: Aborting saving a new resource
Given "http://google.com" is a valid link
When I insert "http://google.com" as the new resource link
And I insert anything as the new resource title
Then I should see a confirmation message with the inserted link and title
When I abort
Then I should have the same number of resources as before
Scenario: Saving a resource with invalid link
Given "http://invalid.link" is an invalid link
When I insert "http://invalid.link" as the new resource link
And I insert anything as the new resource title
Then I should see a confirmation message with the inserted link and title
When I accept
Then I should see an error message telling me that the inserted link is invalid
Scenario: Saving a resource with already saved link
Given "http://google.com" is an already saved link
When I insert "http://google.com" as the new resource link
And I insert anything as the new resource title
Then I should see a confirmation message with the inserted link and title
When I accept
Then I should see an error message telling me that the inserted link already exists
如您所见,在多个场景中重复了很多样板文件。我知道我可以定义一个 Background
,其中包含在所有场景之前要执行的步骤列表,我可以将所有步骤放在确认消息之前,但是如果我这样做,我将无法区分在用户可能插入的不同链接和标题中。
是否可以定义一个仅用于某些场景而不是所有场景的背景?或者可能连接两个场景,例如要求某个场景(我可以重复使用)在另一个场景之前是 运行?还是我应该继续重复样板文件?
我会考虑考虑你的场景应该做什么。目前,我看到的脚本讨论了如何完成工作。几乎没有什么应该做的。
导航详细信息对于理解系统应该做什么并不是很有用。这是一个众所周知的初学者错误,如 [1] 中所述并转录于 [2]。
您要做的是将 UI 详细信息压入堆栈。导航详细信息在您实施的步骤使用的助手 methods/classes 中生活得更好。页面对象是从场景中隐藏导航的一种方式。
当您去掉导航细节时,您的一些重复项将会消失。出于可读性原因,剩余的重复可能是可以接受的。
请记住,理解场景比一点点重复重要得多。
[1] https://cucumber.io/blog/2016/05/09/cucumber-antipatterns
[2] http://www.thinkcode.se/blog/2016/06/22/cucumber-antipatterns
继 Thomas 的回答后
您的方案复杂且重复,因为每次它们描述 'how' 用户都与应用程序交互。 'How' 在场景中没有位置,因为
随着您对自己正在做的事情了解得更多,您做事方式的细节可能会发生变化。您不希望每次更改操作细节时都必须更改场景
在您的场景中添加内容会使它们变得无聊、重复、难以阅读且实施起来成本高昂。
描述你的场景通常是一种避免做场景的实际工作的方法,即找出'why'你正在做的事情并优雅和简洁地定义'what'您正在努力实现。
n.b。还有很多其他原因
让我们看看当您完成这些额外的工作后,您的场景会变得多么简单
##旧
Scenario: Saving a new resource
Given "http://google.com" is a valid link
When I insert "http://google.com" as the new resource link
And I insert "Google Search Engine" as the new resource title
Then I should see a confirmation message with the inserted link and title
When I accept
Then I should have 1 additional resource with the inserted link and title
##新
Scenario: Bookmarking
When I save a bookmark
Then my bookmark should be saved
##旧
Scenario: Saving a resource with invalid link
Given "http://invalid.link" is an invalid link
When I insert "http://invalid.link" as the new resource link
And I insert anything as the new resource title
Then I should see a confirmation message with the inserted link and title
When I accept
Then I should see an error message telling me that the inserted link is invalid
##新
Scenario: Bookmark with invalid link
When I bookmark with an invalid link
Then I should see an invalid link error
等等
请注意新场景如何清楚地说明您在增加业务价值方面所做的工作有多么少!!
关于您关于背景的问题,如果您需要两个不同的背景,您需要两个不同的 .feature 文件。这是好事!!