如何编写基于先前功能的 Cucumber 测试?
How Do I Write Cucumber Tests That Build On Previous Features?
我已经开始使用 Cucumber 编写 BDD 测试来匹配我的应用程序的业务用例。
它是基于区块链的,因此测试中的每个用户都是 运行 应用程序的一个实例。这也意味着每次测试都相当繁重,95%的测试时间都在设置阶段。
当我编写测试时,我发现我开始重复自己,而早期的功能似乎变得多余。
一个业务流程是:
- User1 保存了一条新消息
- 用户 2 编辑了消息
- User1 验证编辑
- 用户 1 取消了消息
- 用户 2 确认取消
这被分解为 New/Edit/Cancel 个特征。
最初我从 "New" 和 "Edit" 特征文件开始,如下所示:
新建
Feature: a new message is added
Scenario: a user adds a new message
Given there is a user called User1
And there is a user called User2
When User1 creates a new message with id 1234
Then User2 should see the message with id 1234
编辑
Feature: Editing a message
Scenario: A User edits a message
Given there is a user called User1
And there is a user called User2
When User1 creates a new message with id 1234
And User2 adds the location US to the message
Then User1 should see the location US on the message
但现在我要进入取消部分,我意识到为了正确测试取消,系统需要有一条编辑过的消息,这意味着我需要通过新建和编辑功能才能获得消息进入正确的状态。
这会使取消看起来像这样,然后开始变得很长:
取消
Feature: Cancelling a message
Scenario: A User cancels a message
Given there is a user called User1
And there is a user called User2
When User1 creates a new message with id 1234
And User2 adds the location US to the message
And User1 cancels the message
Then User2 should see status Cancelled on the message
我可以这样写取消:
Feature: Cancelling a message
Scenario: A User cancels a message
Given there is a message with id 1234
And User1 cancels the message
Then User2 should see status Cancelled on the message
作为一个功能读起来非常好,但是,我现在必须为 "there is a message with id 1234" 编写一个步骤定义,它完成编辑功能所做的一切。
如开头所述,这些测试中的设置占用了 95% 的测试时间,因此理想情况下,我希望 运行 将这些作为一系列步骤放在一起,而不是针对每个功能从头开始。
例如
- 设置一次
- 创建新消息
- 编辑消息
- 取消消息
是否可以将场景或功能链接在一起并重用前一个的系统状态?
还是每次都要从头启动系统?
调用构成编辑功能的所有其他 steps/methods 的步骤定义是否是执行取消的正确方法,还是我应该编写大量 And 语句?
我能理解您对此的不满,但无法让后续功能相互构建。每个场景都应该是原子的和可重复的。场景相互依赖的问题是失败的场景会导致后续场景的级联失败。应用程序中的一次失败会触发多次失败的测试,导致您的团队开始认为这些测试是古怪的。
写一个模拟前面场景的步骤没有错——这是正确的做法。在定义这些步骤时,尽可能保持它们的原子性,以便它们非常可组合。
老实说,6 步场景非常好。我建议的唯一更改是制作步骤的 Given
版本。 取消 场景看起来有很多 When
。
Feature: Cancelling a message
Scenario: A User cancels a message
Given there is a user called User1
And there is a user called User2
And User1 created a new message with id 1234
And User2 added the location US to the message
When User1 cancels the message
Then User2 should see status Cancelled
原文: 你可以从每个设置步骤中提取代码并使它们起作用,将函数作为代码调用到步骤(执行与之前相同的任务),并创建一个调用这些函数的新设置步骤,这意味着它们将有一个共享的实现
备选方案:写入a tagged Before hook that knows the state of the system under test,即使只是设置步骤是否已经发生,并使用该信息为那些场景重置系统。
您甚至可以在此处执行健康检查,以确保在系统需要时可以进行完全重置。
或者甚至将这段代码本身放入步骤中,让他们知道如果通过了健康检查(很可能是 if 语句)就可以跳过部分
我已经开始使用 Cucumber 编写 BDD 测试来匹配我的应用程序的业务用例。 它是基于区块链的,因此测试中的每个用户都是 运行 应用程序的一个实例。这也意味着每次测试都相当繁重,95%的测试时间都在设置阶段。
当我编写测试时,我发现我开始重复自己,而早期的功能似乎变得多余。
一个业务流程是:
- User1 保存了一条新消息
- 用户 2 编辑了消息
- User1 验证编辑
- 用户 1 取消了消息
- 用户 2 确认取消
这被分解为 New/Edit/Cancel 个特征。
最初我从 "New" 和 "Edit" 特征文件开始,如下所示:
新建
Feature: a new message is added
Scenario: a user adds a new message
Given there is a user called User1
And there is a user called User2
When User1 creates a new message with id 1234
Then User2 should see the message with id 1234
编辑
Feature: Editing a message
Scenario: A User edits a message
Given there is a user called User1
And there is a user called User2
When User1 creates a new message with id 1234
And User2 adds the location US to the message
Then User1 should see the location US on the message
但现在我要进入取消部分,我意识到为了正确测试取消,系统需要有一条编辑过的消息,这意味着我需要通过新建和编辑功能才能获得消息进入正确的状态。
这会使取消看起来像这样,然后开始变得很长:
取消
Feature: Cancelling a message
Scenario: A User cancels a message
Given there is a user called User1
And there is a user called User2
When User1 creates a new message with id 1234
And User2 adds the location US to the message
And User1 cancels the message
Then User2 should see status Cancelled on the message
我可以这样写取消:
Feature: Cancelling a message
Scenario: A User cancels a message
Given there is a message with id 1234
And User1 cancels the message
Then User2 should see status Cancelled on the message
作为一个功能读起来非常好,但是,我现在必须为 "there is a message with id 1234" 编写一个步骤定义,它完成编辑功能所做的一切。
如开头所述,这些测试中的设置占用了 95% 的测试时间,因此理想情况下,我希望 运行 将这些作为一系列步骤放在一起,而不是针对每个功能从头开始。 例如
- 设置一次
- 创建新消息
- 编辑消息
- 取消消息
是否可以将场景或功能链接在一起并重用前一个的系统状态?
还是每次都要从头启动系统?
调用构成编辑功能的所有其他 steps/methods 的步骤定义是否是执行取消的正确方法,还是我应该编写大量 And 语句?
我能理解您对此的不满,但无法让后续功能相互构建。每个场景都应该是原子的和可重复的。场景相互依赖的问题是失败的场景会导致后续场景的级联失败。应用程序中的一次失败会触发多次失败的测试,导致您的团队开始认为这些测试是古怪的。
写一个模拟前面场景的步骤没有错——这是正确的做法。在定义这些步骤时,尽可能保持它们的原子性,以便它们非常可组合。
老实说,6 步场景非常好。我建议的唯一更改是制作步骤的 Given
版本。 取消 场景看起来有很多 When
。
Feature: Cancelling a message
Scenario: A User cancels a message
Given there is a user called User1
And there is a user called User2
And User1 created a new message with id 1234
And User2 added the location US to the message
When User1 cancels the message
Then User2 should see status Cancelled
原文: 你可以从每个设置步骤中提取代码并使它们起作用,将函数作为代码调用到步骤(执行与之前相同的任务),并创建一个调用这些函数的新设置步骤,这意味着它们将有一个共享的实现
备选方案:写入a tagged Before hook that knows the state of the system under test,即使只是设置步骤是否已经发生,并使用该信息为那些场景重置系统。 您甚至可以在此处执行健康检查,以确保在系统需要时可以进行完全重置。
或者甚至将这段代码本身放入步骤中,让他们知道如果通过了健康检查(很可能是 if 语句)就可以跳过部分