我使用Behat语言定义了使用Cucumber或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
,其中包含要执行的步骤列表,我可以将所有步骤放到确认消息中,但如果我这样做,我将无法区分用户可能插入的不同链接和标题之中。
是否可以定义仅用于特定场景而非所有场景的背景?或者可能连接两个场景,例如要求某个场景(我可以重用)在另一场景之前运行?或者我应该继续重复样板?
答案 0 :(得分:5)
我会考虑考虑你的场景应该做什么。目前我看到的脚本讨论了如何完成工作。几乎没有关于应该做什么的事情。
导航细节对于理解系统应该做什么并不是很有用。这是一个众所周知的初学者错误,如[1]中所述并在[2]中转录。
您要做的是将UI详细信息推送到堆栈中。导航细节在您实现的步骤所使用的辅助方法/类中更好。页面对象是隐藏场景导航的一种方法。
当你摆脱导航细节时,你的一些复制将消失。出于可读性原因,剩余的重复可能是可接受的。
请记住,理解场景比重复更重要。
[1] https://cucumber.io/blog/2016/05/09/cucumber-antipatterns
[2] http://www.thinkcode.se/blog/2016/06/22/cucumber-antipatterns
答案 1 :(得分:4)
继表格托马斯'答案
您的情景既复杂又重复,因为每次他们描述'如何'用户与应用程序交互。 '如何'在场景中没有位置,因为
1)当您更多地了解自己在做什么时,您的工作细节可能会发生变化。每次更改有关操作的详细信息时,您都不希望必须更改方案
2)在你的场景中如何使它们变得枯燥,重复,难以阅读并且实施起来很昂贵。
3)确定你的场景通常是一种避免完成场景所针对的实际工作的方法,即找出“为什么”这样做的原因。你正在做一些事情,并且优雅地定义和简洁地定义什么'你想要实现。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文件。这是件好事!!