gpt4 book ai didi

cucumber - Gherkin 中的分组步骤或连接场景

转载 作者:行者123 更新时间:2023-12-02 06:29:56 27 4
gpt4 key购买 nike

我正在使用 Behat 等工具定义要在 BDD 工作流程中使用的功能。或 Cucumber , 使用 Gherkin语。这是迄今为止的特征定义:

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. 在你的场景中加入如何让它们变得乏味、重复、难以阅读且实现起来很昂贵。

  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

等等

请注意新场景如何清楚地说明您在增加业务值(value)方面所做的工作是多么少!

至于关于背景的问题,如果您需要两个不同的背景,则需要两个不同的 .feature 文件。这是好事!!

关于cucumber - Gherkin 中的分组步骤或连接场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41197085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com