Gherkin"OR"语法减少BDD的重复

Eam*_*yle 6 bdd specflow gherkin

有没有人知道实现这一目标的方法,或者他们认为这是一个好主意.在Gherkin中使用OR样式语法来减少重复但保持人类可读性(希望如此).我正在考虑使用多个OR语句的每个组合扩展子句组合的情况.例如

Scenario: TestCopy
  Given Some text is selected
  When The user presses Ctrl + C
    OR the user right clicks and selects copy
    OR the user selects Edit + Copy
  Then the text is copied to the clipboard
Run Code Online (Sandbox Code Playgroud)

这将作为3个测试运行,每个测试使用相同的给定,然后使用一个来自OR集合.我想这可以使用带有When子句的占位符的模板来实现,但我认为这更具可读性,并且可以允许OR在Given中使用以产生nxm测试.使用大纲,您仍然需要nxm行.

  • 有一个更好的方法吗
  • 明确复制和粘贴是否是更好的做法(我认为维护可能会变得混乱)
  • 其他框架是否支持这一点(我认为使用FIT你可以写一个自定义表但是这看起来像是开销)

谢谢.

nem*_*esv 12

您可以使用场景大纲从一个场景生成多个测试:

Scenario Outline: TestCopy
  Given Some text is selected
  When <Copy operation executed>
  Then the text is copied to the clipboard

Examples: 
    | Copy operation executed                |
    | The user presses Ctrl + C              |
    | the user right clicks and selects copy |
    | the user selects Edit + Copy           |
Run Code Online (Sandbox Code Playgroud)

Scenario Outline你基本上创建一个模板,填写与提供Examples.
对于上面的示例,Specflow将生成3个具有相同GivenThen3个不同Whens的测试:

When The user presses Ctrl + C
When the user right clicks and selects copy
When the user selects Edit + Copy
Run Code Online (Sandbox Code Playgroud)


Mar*_*niz 9

不建议在场景中使用此详细级别(按下这些键,右键单击).正如你所知,这使得它们冗长而重复.此外,这通常不是利益相关者需要或想要的信息.

最好的方法是隐藏步骤定义的实现细节.您的方案将是这样的:

Scenario: TestCopy
  Given some text is selected
  When the user copies the selected text
  Then the selected text is copied to the clipboard
Run Code Online (Sandbox Code Playgroud)

复制文本的不同方法将转到第3步定义.


per*_*ist 5

至于nxm场景,我觉得当你想这样做时,你可能错了。

你没有给出一个明确的例子,但假设你想要这样的东西:

Given A block of text is selected
OR An image is selected
OR An image and some text is selected
When The user presses Ctrl + C
OR the user right clicks and selects copy
OR the user selects Edit + Copy
Run Code Online (Sandbox Code Playgroud)

写你的Then条款将是一场噩梦。

相反,尝试两个测试......首先,正如@nemesv 所建议的那样 - 但将“文本选择”替换为通用“选择”。

Scenario Outline: TestCopy
  Given I have made a selection
  When <Copy operation executed>
  Then my selection is copied to the clipboard

Examples: 
  | Copy operation executed                |
  | The user presses Ctrl + C              |
  | the user right clicks and selects copy |
  | the user selects Edit + Copy           |
Run Code Online (Sandbox Code Playgroud)

然后,您可以编写一个或多个附加测试来处理“什么是有效选择” - 这可能是由您使用的独立于复制功能的功能 - 例如,当您进行选择并点击删除时会发生什么... 或 ctrl-v ... 或拖放?

您不想走这条路,将所有有效的选择方式与您可以采取的所有有效行动相乘。