【问题标题】:Cucumber: Scenario Outline reusing examples tableCucumber:场景大纲重用示例表
【发布时间】:2014-11-03 14:26:10
【问题描述】:
我有一些测试如下:
Scenario Outline: Add two numebrs
Given two numbers <number_1> and <number_2>
When I add them
Then Result is <number_3>
Examples:
|number_1|number_2|number_3|
|2 |3 |5 |
|1 |2 |3 |
Scenario Outline: Update two numebrs
Given two numbers <number_1> and <number_2>
When I update them
Then Result is <number_3>
Examples:
|number_1|number_2|number_3|
|2 |3 |5 |
|1 |2 |3 |
对于每个测试,我应该添加相同的表示例。
有什么方法可以提取此表以对所有测试使用相同的表吗?
【问题讨论】:
标签:
java
cucumber
integration-testing
【解决方案1】:
我想到的最简单的解决方案是将这两种情况结合起来,将详细信息提取到示例表中。所以它看起来像:
| number_1 | number_2 | operation | result |
你还有另一种可能。
Scenario: Add two numebrs
Given I have the matrix of numbers
When I add them
Then I would have the resulting vector.
Scenario: Update two numebrs
Given I have the matrix of numbers
When I update them
Then I would have the resulting vector.
其中“数字矩阵”和“结果向量”转到步骤 defs 文件。
【解决方案2】:
您可以使用qaf-gherkin 在其中移动外部文件中的示例并将其用于一个或多个场景。使用 qaf,您的功能文件可能如下所示:
Scenario Outline: Add two numebrs
Given two numbers <number_1> and <number_2>
When I add them
Then Result is <number_3>
Examples::{'datafile':'resources/testdata.txt'}
Scenario Outline: Update two numebrs
Given two numbers <number_1> and <number_2>
When I update them
Then Result is <number_3>
Examples:{'datafile':'resources/testdata.txt'}
您的数据文件将如下所示:
#col.separator=|
number_1|number_2|number_3
2|3|5
1|2|3
以上是带有 | 的 csv(章程分隔值)数据提供程序的示例作为分隔符。您还可以使用不同的数据提供者来提供来自任何 excel/xml/json/database 的数据。
编辑:qaf-cucumber 支持 BDD2 支持可与 Cumber 5 一起使用的黄瓜。
【解决方案3】:
如果你想使用 cucumber runner,你不能使用 QAF,因为它可以与 TestNG 一起使用。此外,我认为仅仅为了使用数据提供者而切换到 QAF 是矫枉过正的。
您可能希望使用 user861594 所述的 qaf-cucumber(它提供了使用 cucumber runner 和所有 QAF BDD2 功能的选择),但目前该插件处于测试阶段;我对其进行了测试,但它有问题(示例的占位符不起作用,与漂亮不兼容)。希望稳定版本能尽快发布。
我选择的解决方案原则上与 qaf-cucumber 所做的相同:用 cucumber 覆盖作为传递依赖的 Gherkin 编译器,并且只更改它解析场景大纲的方式,以便示例可以从 .csv/.txt 文件中提取。为此,您必须创建类gherkin.pickles.Compiler。这与真正的小黄瓜编译器的路径相同,因此这将覆盖对它的引用。然后你可以复制/粘贴真正的小黄瓜编译器的代码并修改它以满足你的需要。
当然,这不是一个完美的解决方案。例如,如果 gherkin 编译器的路径在版本升级后发生变化,那么编译器的路径也必须改变。
注意:目前,qaf-cucumber 不适用于 Pretty 插件,因为 Pretty 会在打印之前再次解析场景,但不适用于 Compiler 小黄瓜类。这是在 io.cucumber.core.plugin.TestSourcesModel 类中完成的,因此为了完成漂亮的工作,您可能还必须覆盖该类。我都做了,到目前为止效果很好。