为了账号安全,请及时绑定邮箱和手机立即绑定

如何编写基于以前功能的 Cucumber 测试?

如何编写基于以前功能的 Cucumber 测试?

婷婷同学_ 2023-10-12 14:50:13
我已经开始使用 Cucumber 编写 BDD 测试来匹配我的应用程序的业务用例。它基于区块链,因此测试中的每个用户都在运行应用程序的一个实例。这也意味着每次测试都相当繁重,95%的测试时间都在准备阶段。当我编写测试时,我发现我开始重复自己,并且早期的功能似乎变得多余。一种业务流程是:用户1保存一条新消息用户2编辑消息User1 验证编辑用户1取消消息用户2确认取消这分为新建/编辑/取消功能。最初,我从“新建”和“编辑”功能文件开始,如下所示:新的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作为一项功能,读起来相当不错,但是,我现在必须为“有一条 id 1234 的消息”编写一个步骤定义,该定义可以执行“编辑”功能正在执行的所有操作。正如在开始时提到的,这些测试中的设置占用了 95% 的测试时间,因此理想情况下,我希望将这些作为一系列步骤一起运行,而不是从每个功能的新鲜开始。例如进行一次设置创建新消息编辑消息取消留言是否可以将场景或功能链接在一起并重用前一个场景或功能的系统状态?还是每次都必须从头开始启动系统?调用构成“编辑”功能的所有其他步骤/方法的步骤定义是否是“取消”的正确方法,还是应该编写大量 And 语句?
查看完整描述

2 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

我可以理解您对此的不满,但后续功能无法相互构建。每个场景都是原子的且可重复的。场景相互依赖的问题是,失败的场景会导致后续场景中的级联故障。应用程序中的一个故障会触发多个失败的测试,导致您的团队开始认为测试不可靠。

编写一个模拟之前场景的步骤并没有什么问题——这是正确的方法。定义这些步骤时,请尽可能保持它们的原子性,以便它们非常可组合。

老实说,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


查看完整回答
反对 回复 2023-10-12
?
jeck猫

TA贡献1909条经验 获得超7个赞

原始:您可以从每个设置步骤中提取代码并使它们起作用,将函数作为步骤的代码调用(执行与之前相同的任务),然后创建一个调用这些函数的新设置步骤,这意味着他们有一个共享的实现

替代方案:编写一个带标记的 Before 挂钩,它知道被测系统的状态,即使只是设置步骤是否已发生,并使用该信息针对这些情况重置系统。您甚至可以在此处执行运行状况检查,以确保在系统需要时可以进行完全重置。

或者甚至将此代码放入步骤本身中,并让他们知道如果运行状况检查已通过则跳过部分(很可能是 if 语句)


查看完整回答
反对 回复 2023-10-12
  • 2 回答
  • 0 关注
  • 67 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信