小黄瓜“OR”使用 BDD 减少重复的语法

发布于 2025-01-01 11:49:00 字数 631 浏览 0 评论 0原文

有谁知道实现此目标的方法或者他们认为这是一个好主意。在 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 个测试运行,每个测试都具有相同的给定值和 then,但具有来自 OR 集合的一个 When。我想这可以使用带有 When 子句占位符的模板来实现,但我认为这更具可读性,并且可以允许在 Give 中使用 OR 来生成 nxm 测试。使用轮廓,您仍然需要 nxm 行。

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

谢谢。

Does anyone know of a way to achieve this or do they think it's a good idea. To have an OR style syntax in Gherkin for reducing repetition but maintaining human readability (hopefully). I'm thinking of cases where clause combinations are expanded with every combination of multiple OR statements. e.g.

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

This would run as 3 tests each with the same given and then but with one When from the OR set. I guess this could have been acheived using a template with a place holder for the When clause but I think this is more readable and could allow the OR to be used in the Given as well to produce n x m tests. With the outline you would still need n x m rows.

  • Is there a better way to do this
  • Is it better practice to explicitly copy and paste (I'm thinking maintenance could get messy)
  • Do other frameworks support this (I think with FIT you could write a custom table but again this seems like overhead)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒中人 2025-01-08 11:49:00

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

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           |

场景大纲您基本上创建了一个模板,其中填充了提供的示例
对于上面的示例,Specflow 将生成 3 个具有相同 GivenThen 以及 3 个不同 When 的测试:

When The user presses Ctrl + C
When the user right clicks and selects copy
When the user selects Edit + Copy

You can generatd multiple tests from one scenario with Scenario Outlines:

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           |

In a Scenario Outline you basically create a template which is filled in the with the provided Examples.
For the the above example Specflow will generate 3 tests with the same Given and Then and with the 3 different Whens:

When The user presses Ctrl + C
When the user right clicks and selects copy
When the user selects Edit + Copy
怀里藏娇 2025-01-08 11:49:00

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

最好的办法是隐藏步骤定义的实现细节。您的场景将类似于:

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

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

It's not recommended to use this detail level (pressing these keys, right clicking) on the scenarios. This makes them, as you realize, lengthy and repetitive. Also, that's usually not the information the stakeholders would need or want anyway.

The best would be to hide the implementation details on the step definitions. Your scenario would be something like:

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

And the different ways of copying the text would go to the 3rd step definition.

帥小哥 2025-01-08 11:49:00

至于nx m场景,我觉得当你想这样做时,你可能会错。

您没有给出明确的示例,但假设您想要类似的内容:

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...或拖放?

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

As for the n x m scenario, I feel like that when you want to do that, you're probably cuking it wrong.

You didn't give an explicit example, but suppose you want something like:

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

Writing your Then clause will be a nightmare.

Instead, try two tests... first, as suggested by @nemesv - but with "text selection" replaced by a generic "selection".

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           |

You can then write a one or more additional tests to deal with "what makes a valid selection" - and this will probably by a feature that you use independent of the copy function - for example, what happens when you make a selection and hit delete... or ctrl-v... or drag and drop?

You don't want to go down the road of multiplying all the valid ways of making a selection against all the valid actions you can take when you've got one.

不美如何 2025-01-08 11:49:00

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

i'd say that copying and pasting is essentially just making multiple calls to the same method. you are using the same step definititions, so why not just call them multiple times. copy/paste, to me, accomplishes what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文