pytest-dev / pytest-bdd

BDD library for the pytest runner
https://pytest-bdd.readthedocs.io/en/latest/
MIT License
1.31k stars 220 forks source link

Given steps are not running converters for examples #134

Closed itspage closed 9 years ago

itspage commented 9 years ago

I have the following given decorator for a scenario outline with examples.

@given("I have a product with price ", converters=dict(base_price=int))

Inside the function, the converter hasn't run on base_price and it's still a string.

bubenkoff commented 9 years ago

converters are for argumented steps if you mean something like:

from pytest_bdd import parsers
from pytest_bdd import given

@given(parsers.parse("I have a product with {base_price} "), converters=dict(base_price=int))
def i_have_a_product(base_price):
...
bubenkoff commented 9 years ago

in this example with simple int, converter is not even necessary as parsers.parse can do conversion for you via {base_price:d}

olegpidsadnyi commented 9 years ago

The data for the parametrized steps comes from the plain text. Does it make sense to allow converters there as well?

itspage commented 9 years ago

I'm using a simple text step because I have examples, so my feature looks like:

  Scenario Outline: Some Scenario
    Given I have a product with price <base_price>
    When I request the price
    Then the price is <expected_price>

    Examples:
      | base_price | expected_price |
      | 42         | 42             |
      | 35         | 35             | 

And my step looks like:

@given("I have a product with price <base_price>", converters=dict(base_price=int))
bubenkoff commented 9 years ago

@olegpidsadnyi im about argumented, not parametrized steps

bubenkoff commented 9 years ago

@itspage, ok so you use parametrized step and you need to use example_converters on scenario level:

@given("I have a product with price <base_price>")
...

@scenario('some file', 'Some Scenario', example_converters=dict(base_price=int))
...

as in example https://github.com/pytest-dev/pytest-bdd#scenario-outlines

The reason why converters for outlined scenarios are needed on scenario level, and not on step level is that parametrization table (example) is globally accessible during the scenario, and it's important to have it all converted before the execution

itspage commented 9 years ago

Got it, thank you.