cucumber / common

A home for issues that are common to multiple cucumber repositories
https://cucumber.io/docs
MIT License
3.36k stars 692 forks source link

Gherkin parser doesn't interpolate parameters on background level #1580

Closed elchupanebrej closed 2 years ago

elchupanebrej commented 3 years ago

Summary

Gherkin parser doesn't interpolate parameters on background level, so hard to write tests in a DRY manner

Current Behavior

When I execute the next python code (gherkin-official==19.0.2)


data = '''\
Feature: Background example
    Background:
        Given Use <example>

    Rule: 
        Background:
            Given Use <example>    

        Scenario:
            Given Use <example>

        Examples: 
            |example|
            |car         |
'''

parser = Parser()
gherkin_document = parser.parse(data)
gherkin_document['uri'] = None
pickles = Compiler().compile(gherkin_document)
pickles[0]

I got next pickle:

{'astNodeIds': ['8', '6'],
 'id': '3',
 'name': '',
 'language': 'en',
 'steps': [
  {'astNodeIds': ['0'], 'id': '0', 'text': 'Use <example>'},
  {'astNodeIds': ['2'], 'id': '1', 'text': 'Use <example>'},
  {'astNodeIds': ['4', '6'], 'id': '2', 'text': 'Use car'}
],
 'tags': [],
 'uri': None}

Pay attention that the first two steps are not interpolated

Expected Behavior

Both steps (Scenario and background are interpolated)

Possible Solution

In addition to interpolation, it's a good idea to add the possibility to put "Examples:" sections on any level (Feature/Background)

Context & Motivation

I have a big suite of long-running E2E highly parametrized tests. I would like to have the possibility to control the time of test run by selecting specific parametrization for cases on any test suite level. Also, I would like to have parametrization on different levels of Feature/Rule/Scenario to not blow up parameters tables
I use pytest_bdd for my day-to-day activities, but it has no specific features, which this project has(and vice versa). During of integration of the current parser to that project I've found that these two projects using two different dialects of the Gherkin language

aslakhellesoy commented 3 years ago

This is a duplicate of https://github.com/cucumber/common/issues/469 where I have explained why this won’t work

elchupanebrej commented 3 years ago

Thanks for the fast responce! Yes, it's definitely duplicate. Could we just discuss how it could work because 3 years were passed since #469 There you said that people would mix Scenario and Scenario Outlines, but now there is no big difference between Scenario, Example, Scenario Outline, and Scenario Template. Everything from this could be used for parameterized tests.

Let assume that we already have this feature in place. It's definitely an extension of language. How it could be mixed? Let me show

Feature: Fruit operations
    Background:
         Given I have a <fruit>

   Scenario:
          Then I could put it to <tableware>

   @small
    Examples:
       |fruit|tableware|
       |apple|plate|
       |cherry|cup|

    @big
    Examples:
       |fruit|tableware|
       |watermellon|basket|

    Scenario:
        Then I could wash it

    Examples:
       |fruit|
       |apple|
       |cherry|
       |watermellon|

or even

Feature: Fruit operations
    Background:
         Given I have a <fruit>

    Examples:
       |fruit|
       |apple|
       |cherry|
       |watermellon|

   Scenario:
          Then I could put it to <tableware>

    Examples:
       |tableware|
       |plate|
       |cup|

    Scenario:
        Then I could wash it

How it could be executed: Every block(Background, Rule, Scenario) could have an Example table(with a selected set of parameters) Every Route contained from Steps is parametrized. For every flow, we could get a set of parameters and a set of affected Example tables. To marshall the Route to Pickle set we do next: we make a cartesian product of every Example Table and interpolate Route steps with every variant.

aslakhellesoy commented 3 years ago

Ok, what would you expect the placeholder to be for the first scenario here:


Feature: Fruit operations
    Background:
         Given I have a <fruit>

    Scenario: No examples, just a regular scenario
      When I go to town

   Scenario:
          Then I could put it to <tableware>

   @small
    Examples:
       |fruit|tableware|
       |apple|plate|
       |cherry|cup|

    @big
    Examples:
       |fruit|tableware|
       |watermellon|basket|

    Scenario:
        Then I could wash it

    Examples:
       |fruit|
       |apple|
       |cherry|
       |watermellon|
elchupanebrej commented 3 years ago

This scenario will count as parameterized and has to provide Example table with some "fruit". If it's not provided parser has to throw exception with message that Examples are expected

aslakhellesoy commented 3 years ago

Implementing this would have significant ramifications. There are several Gherkin parser implementations to modify (Java, Ruby, TypeScript, Go, C#, Elixir, Python, Perl...)

It’s not a priority for us to undertake this massive piece of work, so I’m afraid you’d have to contribute a pull request for all those languages in order to get it accepted.

Are you up for the task? 😉

elchupanebrej commented 3 years ago

Not ready now) But at least I would try to implement this for python. Thanks for discussion!

aslakhellesoy commented 3 years ago

Ok @elchupanebrej - the place to start would be in https://github.com/cucumber/common/blob/main/gherkin/python/gherkin/pickles/compiler.py.

Let's keep this open for a couple of months. As we aim to keep all Gherkin implementations consistent, it would have to be ported to all the other languages we support.

mpkorstanje commented 2 years ago

Let's keep this open for a couple of months.

It's been open for a few months.