在一个 Cucumber 场景中有多个 Then 是否正确?

Is it correct to have more than one Then in a single Cucumber scenario?

我是 Cucumber 的新手。我正在尝试编写一个测试

的功能文件
  1. 现有页面上的新下拉菜单
  2. 并在选择此新值条目时进入新数据库 Table

我写了这个功能文件。好像不太对。

特征文件就像

Scenario: Admin user should be able to assign ReadOnly role to a searched user via  Change User page

Given user logs into webapp with Admin role 
And Navigates to Change User page 

When user searches for user with id 123 
And clicks select link corresponding to correct id

Then Change User page loads 
And it has a new drop down with Read Only role
And when user selects MS distributor in drop down  # note when with small w
And Presses Submit button then a new entry is saved in DB table # then with small t

或者我可以使用以下方法:

Scenario: Admin user should be able to assign ReadOnly role to a searched user via  Change User page

Given user logs into webapp with Admin role 
And Navigates to Change User page 

When user searches for user with id 123 
And clicks select link corresponding to correct id to open Change User page
And it has a new drop down with Read Only role # need to check this new value in my selenium test case
And when user selects MS distributor value in drop down  # note when with small w
And presses Save button 

Then a new entry is saved in DB table

我期待着从您的经验中学习。

根据我目前的理解,我会像这样写你的场景:

Scenario: Admin user should be able to assign ReadOnly role to a searched user via Change User page
    When I log in as an admin
    And I navigate to the Change User page
    And I search for the user with ID 123 
    And I click the link to user 123's Change User page
    And I select the MS distributor value from the dropdown with the Read Only role
    And I press the Save button 
    Then a new entry is saved in the database table

要点是没有必要断言下拉列表存在。就用它吧。如果下拉列表不存在,场景将失败。

When(以及 When 之后的 And)用于用户操作。 Then(以及 Then 之后的 And)用于断言。在一个场景中有多个 When/Then 部分是可以的;这里没有必要。我喜欢在第二个和后面的 When 之前有一个空行,这样可以更容易地看到有多个这样的部分。

其他要点:

  • 每一步都要有明确的主语,即句子的主语:"the user"或"I"。为了简洁起见,我使用 "I" 而不是 "the user"。全程使用"the user"就好了。

  • 我会在前两个步骤中使用 When 而不是 Given,因为它们的措辞就好像它们是用户执行的一系列操作的一部分服用。或者,您可以编写两个步骤,措辞就好像他们所说的已经如此,并将它们与 Given:

    一起使用
    Given that I am logged in as an admin
    And I am on the Change User page
    
  • "a new entry is saved in the database table" 含糊不清,对于场景语言来说太具体了。改写具体的措辞,并用商业术语说明正在发生的事情。

当您完成编写代码并且一切正常时,某人(或自动化系统之类的东西)将能够做什么他们以前做不到的事情?

你能举出这样的例子吗?

我很确定不会将字段放入数据库。除非其他人正在使用该数据库,否则他们的能力将是读取您刚输入的信息

你在验收标准中告诉我的是这样的:

Admin user should be able to assign ReadOnly role to a searched user via  Change User page

你能举个例子吗?你能给我一个管理员用户的例子吗?他们能做什么?还是做不到?用户不能做什么?你能给我一个他们可能想要编辑的条目的例子吗?

每当我看到任何通用的东西(例如"a user")时,我都会请您举个例子。给我一个名字!即使它是一个愚蠢的、有趣的。

这是我的意思(示例):

Given Andy Admin has privileges for changing users
And Reggie Writer is a user with privileges for writing entries
And a page on "Dancing with Unicorns" is editable to writers
When Andy Admin changes Reggie Writer's privileges to be Read Only
Then Reggie Writer should no longer be able to edit the page "Dancing with Unicorns".

您会发现语言与实现无关。您无法判断 Andy 和 Reggie 是在使用网页、手机上的应用程序 phone 还是老式的 DOS 提示符。这是关于问题,而不是解决方案。数据库通常是解决问题的一种方式。

此外,除非实际使用,否则数据库条目没有价值。一个场景必须有一个有价值的结果。

当然可以有不止一个:

Given Martin Moneybags has plenty of money <-- we could define how much if we wanted
And he's authenticated himself with the cash machine
When he asks for £1000
Then the cash machine should give him £1000
And it should debit his account by £1000.

毕竟,你不会想忘记最后一个。

Given多一个,Then多一个完全OK

有些人有一个规定,当时,应该只有一个。但是,如果重要的行为是关于两个人的动作的交互,或者时间流逝之类的交互,那么多一个也可以。不过,这是一种罕见的情况。

如果您的步骤超过七个,请查看是否有一些非常常见的背景或结果,您可以将其转移到不同的功能中。例如,我希望看到所有涉及人们透支取款的场景都具有不同的功能,即使他们可能共享一些相同的步骤。

比什么都重要的是与理解问题的人交谈。请他们举个例子。有一个谈话。如果你是为自己做这件事,至少要和一只橡皮鸭说话。它是您希望与未来的您进行对话的替身。

如果您不进行对话,那么您就没有进行 BDD。