Skip to content

Commit

Permalink
Add test to make sure that converter for parser for empty strings in …
Browse files Browse the repository at this point in the history
…example tables works as expected

Resolves #551
  • Loading branch information
jsa34 committed Nov 30, 2024
1 parent 93e446e commit dd3266d
Showing 1 changed file with 87 additions and 25 deletions.
112 changes: 87 additions & 25 deletions tests/feature/test_outline_empty_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,6 @@

from pytest_bdd.utils import collect_dumped_objects

STEPS = """\
from pytest_bdd import given, when, then, parsers
from pytest_bdd.utils import dump_obj
# Using `parsers.re` so that we can match empty values
@given(parsers.re("there are (?P<start>.*?) cucumbers"))
def _(start):
dump_obj(start)
@when(parsers.re("I eat (?P<eat>.*?) cucumbers"))
def _(eat):
dump_obj(eat)
@then(parsers.re("I should have (?P<left>.*?) cucumbers"))
def _(left):
dump_obj(left)
"""


def test_scenario_with_empty_example_values(pytester):
pytester.makefile(
Expand All @@ -44,14 +22,36 @@ def test_scenario_with_empty_example_values(pytester):
"""
),
)
pytester.makeconftest(textwrap.dedent(STEPS))
pytester.makeconftest(
textwrap.dedent(
"""\
from pytest_bdd import given, when, then, parsers
from pytest_bdd.utils import dump_obj
# Using `parsers.re` so that we can match empty values
@given(parsers.re("there are (?P<start>.*?) cucumbers"))
def _(start):
dump_obj(start)
@when(parsers.re("I eat (?P<eat>.*?) cucumbers"))
def _(eat):
dump_obj(eat)
@then(parsers.re("I should have (?P<left>.*?) cucumbers"))
def _(left):
dump_obj(left)
"""
)
)

pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd.utils import dump_obj
from pytest_bdd import scenario
import json
@scenario("outline.feature", "Outlined with empty example values")
def test_outline():
Expand All @@ -62,3 +62,65 @@ def test_outline():
result = pytester.runpytest("-s")
result.assert_outcomes(passed=1)
assert collect_dumped_objects(result) == ["#", "", ""]


def test_scenario_with_empty_example_values_none_transformer(pytester):
pytester.makefile(
".feature",
outline=textwrap.dedent(
"""\
Feature: Outline
Scenario Outline: Outlined with empty example values and transformer
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| # | | |
"""
),
)
pytester.makeconftest(
textwrap.dedent(
"""\
from pytest_bdd import given, when, then, parsers
from pytest_bdd.utils import dump_obj
def empty_to_none(value):
return None if value.strip() == "" else value
@given(parsers.re("there are (?P<start>.*?) cucumbers"), converters={"start": empty_to_none})
def _(start):
dump_obj(start)
@when(parsers.re("I eat (?P<eat>.*?) cucumbers"), converters={"eat": empty_to_none})
def _(eat):
dump_obj(eat)
@then(parsers.re("I should have (?P<left>.*?) cucumbers"), converters={"left": empty_to_none})
def _(left):
dump_obj(left)
"""
)
)

pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import scenario
@scenario("outline.feature", "Outlined with empty example values and transformer")
def test_outline():
pass
"""
)
)
result = pytester.runpytest("-s")
result.assert_outcomes(passed=1)
assert collect_dumped_objects(result) == ["#", None, None]

0 comments on commit dd3266d

Please sign in to comment.