6

有谁知道实现这一目标的方法,或者他们认为这是一个好主意。在 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

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

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

谢谢。

4

4 回答 4

14

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

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           |

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
于 2012-02-01T10:14:30.997 回答
9

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

最好的办法是在步骤定义中隐藏实现细节。您的情况将类似于:

Scenario: TestCopy
  Given some text is selected
  When the user copies the selected text
  Then the selected text is copied to the clipboard

复制文本的不同方式将进入第三步定义。

于 2012-02-01T10:06:42.347 回答
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

写你的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           |

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

您不想走上将所有有效的选择方法与您可以采取的所有有效操作相乘的道路。

于 2012-02-01T13:13:20.097 回答
0

我想说复制和粘贴本质上只是对同一方法进行多次调用。您正在使用相同的步骤定义,所以为什么不多次调用它们。复制/粘贴,对我来说,完成你想要的。

于 2012-02-19T01:37:55.663 回答