BDD:我应该向单个场景添加多个给定和结果,还是根据结果拆分场景?

BDD: Should I add multiple givens and outcomes to a single scenario or split scenarios based on outcome?

给定一个测试向第 3 方 API 发送消息的场景,我可以为消息的每个 属性 添加多个给定和结果到单个场景。这使得场景相当复杂。

我也可以将它们分解成不同的场景。但它们确实没有什么不同场景.

这是一个具有多个条件和结果的场景:

Scenario 1: An order
  Given an order
  And that has order ID equal to 42
  And that has affiliate reference equal to foo
  When the conversion for the order is sent
  Then the conversion has an ID equal to 42
  And the conversion has an affiliate ID equal to foo

这里我把它分成了多个场景:

Scenario 1: An order with a specific order ID
  Given an order that has order ID equal to 42
  When the conversion for the order is sent
  Then the conversion has an ID equal to 42

Scenario 2: An order with a specific affiliate reference
  Given an order that has affiliate reference equal to foo
  When the conversion for the order is sent
  Then the conversion has an affiliate ID equal to foo

尝试与公司内的某个人就订单进行对话。向他们询问具有关联参考的订单类型的示例。

如果他们自然而然地谈论具有特定 ID 和联属网络营销参考的订单,并且将这两件事放在一起,则可以将其放在一个场景中。您可能会听到他们在同一个子句中谈论这两件事,例如:

Bus: So, when we send the order for conversion, it should have the same ID and affiliate reference.

MvO: Can you give me an example of those? The ID and affiliate reference?

Bus: Sure, an ID is a simple integer, so, 42, and the affiliate reference is the name of our affiliate, so something like 'Foo'.

(顺便说一句,如果可以的话,请使用真实的会员名称 - 如果您遗漏了什么,企业更容易发现!)

当我们将其转换为 Gherkin 时,语言尽可能自然 (I wrote a blog post on this),我们得到如下内容:

Given an order with ID 42 and affiliate reference "foo"
When we send the order for conversion
Then the conversion should have the same ID and affiliate reference.

但是,如果有些订单没有联属网络营销参考,或者保留联属网络营销参考是完全独立的功能并且业务单独讨论它,那么您可能需要两种情况。

请注意,与您的业务代表交谈还有其他一些好处!

首先,他们可能会用主动语态(我们发送命令)而不是被动语态(命令已发送)来表达 "when",这样更容易看出谁在做什么。这在具有多个角色的场景中尤为重要,可以帮助我们思考谁或什么触发了结果。 (Here's a blog post about tenses and voices in BDD.)

其次,你有机会质疑他们! "Are there any orders which don't have affiliate references? Do all orders have IDs like that, or do you have some old orders with old-style IDs floating around in the system?" 等等。如果您想不出容易提出的问题,请带上测试人员。测试人员非常善于思考要问的问题。 (I wrote a blog post on this, too.)

第三,您更有可能将业务使用的相同语言带入代码中,因此维护起来会更容易,您也将能够更轻松地就它进行对话。

如果您的企业实际上对围绕 API 做什么的对话不感兴趣,那么 不要将基于 Gherkin 的工具用于 API 测试.你可以维护 a little DSL in plain old XUnit 比用英语容易得多。

更笼统地回答你的问题:是的,在一个场景中有多个给定和结果很好。我通常认为,一旦您完成了七个以上的步骤,您就会希望将其拆分为不同的场景。

不过,请确保围绕场景进行对话,因为当您这样做时,很多问题都会消失。