黄瓜:场景大纲重用示例表

时间:2014-11-03 14:26:11

标签: java cucumber integration-testing

我有几个测试如下:

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       |

对于每个测试,我应该添加相同的表示例

有没有办法提取这个表来对所有测试使用相同的表?

3 个答案:

答案 0 :(得分: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文件。

答案 1 :(得分:0)

您可以使用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中的任何数据。

答案 2 :(得分:0)

如果要使用黄瓜浇头,则不能使用QAF,因为它可以与TestNG一起使用。另外,我认为仅使用数据提供程序就切换到QAF是过大的。

您可能要按照user861594的说明使用qaf-cucumber(它提供了使用黄瓜运行器和所有QAF BDD2功能的选择),但是目前该插件处于beta版;我测试了它,发现它有问题(示例的占位符不起作用,与pretty不兼容)。希望很快会发布稳定的版本。

我选择的解决方案在原则上与qaf-cucumber的解决方案相同:使用黄瓜的传递依赖项覆盖G​​herkin编译器,并且仅更改其解析方案大纲的方式,因此示例可以从.csv / .txt文件中提取。为此,您必须创建类gherkin.pickles.Compiler。该路径与真正的gherkin编译器相同,因此将覆盖对其的引用。然后,您可以复制/粘贴真正的小黄瓜编译器的代码,并对其进行修改以适合您的需求。

当然,这不是一个完美的解决方案。例如,如果在版本升级后小黄瓜编译器的路径发生了变化,那么您的编译器的路径也将不得不发生变化。

注意:目前,qaf-cucumber不能与pretty插件一起使用,因为pretty在打印之前重新解析了方案,但不适用于小黄瓜的Compiler类。这是在类io.cucumber.core.plugin.TestSourcesModel中完成的,因此要进行漂亮的工作,您可能还必须覆盖该类。我都做到了,到目前为止一切正常。

相关问题