Cucumber 小黄瓜中的分组步骤或连接场景

Cucumber 小黄瓜中的分组步骤或连接场景,cucumber,bdd,behat,gherkin,scenarios,Cucumber,Bdd,Behat,Gherkin,Scenarios,我正在使用诸如或之类的工具,使用该语言定义一个要在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 Give

我正在使用诸如或之类的工具,使用该语言定义一个要在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
,在所有场景之前执行一系列步骤,我可以将所有步骤放到确认消息中,但如果我这样做,我将无法区分用户可能插入的不同链接和标题


是否可以定义一个仅用于特定场景而非所有场景的背景?或者连接两个场景,例如要求在另一个场景之前运行某个场景(我可以重用)?或者我应该简单地重复一下样板吗?

< P>我会考虑你的场景应该做什么。目前我看到的脚本讲述了如何完成工作。对于应该做什么几乎一无所知

导航详细信息对于理解系统应该做什么并不真正有用。这是一个众所周知的初学者错误,如[1]所述,并在[2]中转录

您要做的是将UI详细信息下推到堆栈中。导航详细信息在您实现的步骤所使用的帮助器方法/类中更有效。页面对象是从场景中隐藏导航的一种方法

当您删除导航详细信息时,您的一些复制将消失。出于可读性原因,剩余的副本可能是可以接受的

记住,理解场景比一点重复要重要得多

[1]


[2] 以下是托马斯的回答

您的场景是复杂和重复的,因为每次它们都在描述用户如何与应用程序交互“如何”在场景中没有位置,因为

1) 随着你对所做事情了解的加深,你做事的细节可能会发生变化。你不想每次改变你做事的细节时都要改变你的情景

2) 在你的场景中加入“如何”会使它们变得枯燥、重复、难以阅读且实施成本高昂

3) 把你的场景放在哪里通常是避免做场景所要做的真正工作的一种方式,这就是找出你在做什么的“原因”,并优雅而简洁地定义你想要实现的“目标”

n、 还有很多其他原因

让我们看看当你做这额外的工作时,你的场景变得多么简单

古老的 新的 古老的 新的 等等

请注意,新的场景如何如此清晰地说明您在增加业务价值方面所做的工作是多么少


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

谢谢,非常有趣的链接!我假设导航(在本例中,存在确认/错误消息)很重要,需要与利益相关者达成一致,因此应该在场景中定义导航。如果我将其放在较低级别的实现中,涉众在构建原型或MVP之前不会知道它,除非他们同意UI设计,在这种情况下,特性的定义有两个不同的来源:场景和UI设计。这是可以接受的吗?UI细节可能很重要,但大多数业务人员都在云端操作。这是从一个非常高的角度来看的。他们通常不关心确认对话框。因此,这样的细节对于我们正在努力实现的对业务有价值的目标并不重要。感谢您的详细回答!保留Thomas的链接作为参考,我发现添加大量细节会让人分心,无法传达该功能的真实行为。然而,也有太高层次和抽象的风险:我不应该在我的场景中用真实的值(在本例中是真实的资源数据)来举例,使它们具体化吗?例如,你对书签场景的建议,对我来说,感觉有点抽象,甚至是重言式的。在反刍时,从重言式开始实际上是一件很好的事情。这是一种简洁地指定快乐路径的好方法,它要求您在计算中做一件最困难也是最有益的事情,那就是为某些东西找到一个名称(在您的示例中是书签)。一旦您开始探索可悲的路径和功能扩展(例如,在一台设备上创建的书签在另一台设备上可用),这些场景将更加丰富。)
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