Cucumber: Scenario Outline reusing examples table

2020-02-07 03:51发布

问题:

I have a few tests like below:

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       |

For each test I should add the same table Examples.

Is any way to extract this table to use the same one for all tests?

回答1:

The easiest solution that comes to my mind is combining both scenarios, extracting details to the examples table. So it would look like:

| number_1 | number_2 | operation | result |

You have another possibility.

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.

Where "the matrix of numbers" and "the resulting vector" go to step defs file.



回答2:

You can use qaf-gherkin where you can move examples in external file and use it with one or more scenario. With qaf your feature file may look like below:

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'}

And your datafile will look like:

  #col.separator=|
  number_1|number_2|number_3
  2|3|5       
  1|2|3       

Above is example of csv (charter separated values) data provider with | as seperator. You also can use different data providers to provide data from any of excel/xml/json/database.

EDIT: qaf-cucumber has BDD2 support with cucumber that can be used with Cumber 5.



回答3:

If you want to use the cucumber runner, you can not use QAF as it works with TestNG. Also I think that switching to QAF just to use data providers is overkill.

You might want to use qaf-cucumber as stated by user861594 (it offers the choice to use the cucumber runner and all QAF BDD2 features), but for the moment the plugin is in beta; I tested it and it is buggy (placeholders for examples not working, incompatible with pretty). Hopefully a stable release will come soon.

The solution I chose is in principle the same as what qaf-cucumber does: overwrite the Gherkin compiler coming as a transitive dependency with cucumber, and only change how it parses scenario outline so examples can be extracted from .csv/.txt files. To do so you have to create the class gherkin.pickles.Compiler. This is the same path than the real gherkin compiler, so this will overwrite the reference to it. Then you can copy/paste the code of the real gherkin compiler and modify it to suit your needs.

Of course this not a perfect solution. For example, if the path to the gherkin compiler changes after a version upgrade, the path to your compiler will have to change too.

Note: For the moment, qaf-cucumber does not work with the pretty plugin because pretty parses the scenario again before printing it, but not with the Compiler class of gherkin. This is done in the class io.cucumber.core.plugin.TestSourcesModel, so to make pretty work you may also have to overwrite this class. I did both and it works fine so far.