在每种情况下启用设置的正确方法是什么

What is the correct way to enable a setting in each scenario

我们有一个设置可以启用对已查看对象的跟踪,但必须将其打开。所以我想把它放在背景中。但是,有一个场景应该在该选项尚未启用并且单击显示已查看对象的按钮时警告用户。解决此问题的最佳方法是什么?

选项 1:使用背景,但据我所知,在场景中无法让它不触发。

Background: Turn on tracking of objects
Given the user is on the settings page
When tracking is enabled
Then the user should see tracking is enabled

选项 2:在每个场景开始时使用类似这样的内容:

Given ...
And tracking of viewed objects is on
When ...
Then ...

我不反对这样做,但所有这些重复让我觉得有更好的方法来做到这一点。

选项 3:将一个场景放在不同的功能文件中。

我最好将有关跟踪的所有内容都放在一个文件中,但如果这是最佳做法...

如果您使用的是最新版本的 Cucumber,则可以使用 the Rule keyword。规则允许您在要素文件中对 scenarios/example 进行分组。每个规则都可以有自己的背景。

Feature: Overdue tasks
  Let users know when tasks are overdue, even when using other
  features of the app

  Rule: Users are notified about overdue tasks on first use of the day
    Background:
      Given I have overdue tasks

    Example: First use of the day
      Given I last used the app yesterday
      When I use the app
      Then I am notified about overdue tasks

    Example: Already used today
      Given I last used the app earlier today
      When I use the app
      Then I am not notified about overdue tasks
  ...

注意:规则与 Example Mapping 结合使用时效果最佳。如果您只是因为需要共享背景而使用它们来对您的功能进行分组,则可能会变得混乱。