Skip to content

Commit

Permalink
Merge pull request #536 from pytest-dev/anonymous-step-funcs
Browse files Browse the repository at this point in the history
Anonymous step funcs
  • Loading branch information
youtux authored Jul 15, 2022
2 parents adcc48f + d73fa1e commit c0357d5
Show file tree
Hide file tree
Showing 26 changed files with 158 additions and 158 deletions.
2 changes: 1 addition & 1 deletion pytest_bdd/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def pytest_addhooks(pluginmanager: PytestPluginManager) -> None:
@given("trace")
@when("trace")
@then("trace")
def trace() -> None:
def _() -> None:
"""Enter pytest's pdb trace."""
pytest.set_trace()

Expand Down
10 changes: 5 additions & 5 deletions pytest_bdd/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
Example:
@given("I have an article", target_fixture="article")
def given_article(author):
def _(author):
return create_test_article(author=author)
@when("I go to the article page")
def go_to_the_article_page(browser, article):
def _(browser, article):
browser.visit(urljoin(browser.url, "/articles/{0}/".format(article.id)))
@then("I should not see the error message")
def no_error_message(browser):
def _(browser):
with pytest.raises(ElementDoesNotExist):
browser.find_by_css(".message.error").first
Expand All @@ -22,15 +22,15 @@ def no_error_message(browser):
@given("I have an article")
@given("there is an article")
def article(author):
def _(author):
return create_test_article(author=author)
Reusing existing fixtures for a different step name:
@given("I have a beautiful article")
def given_beautiful_article(article):
def _(article):
pass
"""
Expand Down
2 changes: 1 addition & 1 deletion pytest_bdd/templates/test.py.mak
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_${ make_python_name(scenario.name)}():
% endfor
% for step in steps:
@${step.type}(${ make_string_literal(step.name)})
def ${ make_python_name(step.name)}():
def _():
${make_python_docstring(step.name)}
raise NotImplementedError
% if not loop.last:
Expand Down
12 changes: 6 additions & 6 deletions tests/args/cfparse/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ def values():
@given(parsers.cfparse("I have {euro:d} Euro"))
def i_have(euro, values):
def _(euro, values):
assert euro == values.pop(0)
@when(parsers.cfparse("I pay {euro:d} Euro"))
def i_pay(euro, values, request):
def _(euro, values, request):
assert euro == values.pop(0)
@then(parsers.cfparse("I should have {euro:d} Euro"))
def i_should_have(euro, values):
def _(euro, values):
assert euro == values.pop(0)
"""
Expand Down Expand Up @@ -89,17 +89,17 @@ def arguments():
@given(parsers.cfparse("I have an argument {arg:Number}", extra_types=dict(Number=int)))
def argument(arguments, arg):
def _(arguments, arg):
arguments["arg"] = arg
@when(parsers.cfparse("I get argument {arg:d}"))
def get_argument(arguments, arg):
def _(arguments, arg):
arguments["arg"] = arg
@then(parsers.cfparse("My argument should be {arg:d}"))
def assert_that_my_argument_is_arg(arguments, arg):
def _(arguments, arg):
assert arguments["arg"] == arg
"""
Expand Down
12 changes: 6 additions & 6 deletions tests/args/parse/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ def values():
@given(parsers.parse("I have {euro:d} Euro"))
def i_have(euro, values):
def _(euro, values):
assert euro == values.pop(0)
@when(parsers.parse("I pay {euro:d} Euro"))
def i_pay(euro, values, request):
def _(euro, values, request):
assert euro == values.pop(0)
@then(parsers.parse("I should have {euro:d} Euro"))
def i_should_have(euro, values):
def _(euro, values):
assert euro == values.pop(0)
"""
Expand Down Expand Up @@ -87,17 +87,17 @@ def test_arguments():
@given(parsers.parse("I have an argument {arg:Number}", extra_types=dict(Number=int)))
def argument(arguments, arg):
def _(arguments, arg):
arguments["arg"] = arg
@when(parsers.parse("I get argument {arg:d}"))
def get_argument(arguments, arg):
def _(arguments, arg):
arguments["arg"] = arg
@then(parsers.parse("My argument should be {arg:d}"))
def assert_that_my_argument_is_arg(arguments, arg):
def _(arguments, arg):
assert arguments["arg"] == arg
"""
Expand Down
12 changes: 6 additions & 6 deletions tests/args/regex/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ def values():
return [1, 2, 1, 0, 999999]
@given(parsers.re(r"I have (?P<euro>\d+) Euro"), converters=dict(euro=int))
def i_have(euro, values):
def _(euro, values):
assert euro == values.pop(0)
@when(parsers.re(r"I pay (?P<euro>\d+) Euro"), converters=dict(euro=int))
def i_pay(euro, values, request):
def _(euro, values, request):
assert euro == values.pop(0)
@then(parsers.re(r"I should have (?P<euro>\d+) Euro"), converters=dict(euro=int))
def i_should_have(euro, values):
def _(euro, values):
assert euro == values.pop(0)
"""
Expand Down Expand Up @@ -139,17 +139,17 @@ def test_arguments():
pass
@given(parsers.re(r"I have an argument (?P<arg>\d+)"))
def argument(arguments, arg):
def _(arguments, arg):
arguments["arg"] = arg
@when(parsers.re(r"I get argument (?P<arg>\d+)"))
def get_argument(arguments, arg):
def _(arguments, arg):
arguments["arg"] = arg
@then(parsers.re(r"My argument should be (?P<arg>\d+)"))
def assert_that_my_argument_is_arg(arguments, arg):
def _(arguments, arg):
assert arguments["arg"] == arg
"""
Expand Down
8 changes: 4 additions & 4 deletions tests/feature/test_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ def test_alias():
@given("I have an empty list", target_fixture="results")
def results():
def _():
return []
@given("I have foo (which is 1) in my list")
@given("I have bar (alias of foo) in my list")
def foo(results):
def _(results):
results.append(1)
@when("I do crash (which is 2)")
@when("I do boom (alias of crash)")
def crash(results):
def _(results):
results.append(2)
@then("my list should be [1, 1, 2, 2]")
def check_results(results):
def _(results):
assert results == [1, 1, 2, 2]
"""
)
Expand Down
14 changes: 7 additions & 7 deletions tests/feature/test_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,40 @@ def foo():
@given(parsers.re(r"a background step with multiple lines:\n(?P<data>.+)", flags=re.DOTALL))
def multi_line(foo, data):
def _(foo, data):
assert data == "one\ntwo"
@given('foo has a value "bar"')
def bar(foo):
def _(foo):
foo["bar"] = "bar"
return foo["bar"]
@given('foo has a value "dummy"')
def dummy(foo):
def _(foo):
foo["dummy"] = "dummy"
return foo["dummy"]
@given('foo has no value "bar"')
def no_bar(foo):
def _(foo):
assert foo["bar"]
del foo["bar"]
@then('foo should have value "bar"')
def foo_has_bar(foo):
def _(foo):
assert foo["bar"] == "bar"
@then('foo should have value "dummy"')
def foo_has_dummy(foo):
def _(foo):
assert foo["dummy"] == "dummy"
@then('foo should not have value "bar"')
def foo_has_no_bar(foo):
def _(foo):
assert "bar" not in foo
"""
Expand Down
8 changes: 4 additions & 4 deletions tests/feature/test_cucumber_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ def test_step_trace(testdir):
from pytest_bdd import given, when, scenario, parsers
@given('a passing step')
def a_passing_step():
def _():
return 'pass'
@given('some other passing step')
def some_other_passing_step():
def _():
return 'pass'
@given('a failing step')
def a_failing_step():
def _():
raise Exception('Error')
@given(parsers.parse('type {type} and value {value}'))
def type_type_and_value_value():
def _():
return 'pass'
@scenario('test.feature', 'Passing')
Expand Down
2 changes: 1 addition & 1 deletion tests/feature/test_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_description():
@given("I have a bar")
def bar():
def _():
return "bar"
def test_scenario_description():
Expand Down
24 changes: 12 additions & 12 deletions tests/feature/test_gherkin_terminal_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
@given('there is a bar')
def a_bar():
def _():
return 'bar'
@when('the bar is accessed')
def the_bar_is_accessed():
def _():
pass
@then('world explodes')
def world_explodes():
def _():
pass
Expand Down Expand Up @@ -123,16 +123,16 @@ def test_error_message_should_be_displayed(testdir, verbosity):
@given('there is a bar')
def a_bar():
def _():
return 'bar'
@when('the bar is accessed')
def the_bar_is_accessed():
def _():
pass
@then('world explodes')
def world_explodes():
def _():
raise Exception("BIGBADABOOM")
Expand All @@ -157,16 +157,16 @@ def test_local_variables_should_be_displayed_when_showlocals_option_is_used(test
@given('there is a bar')
def a_bar():
def _():
return 'bar'
@when('the bar is accessed')
def the_bar_is_accessed():
def _():
pass
@then('world explodes')
def world_explodes():
def _():
local_var = "MULTIPASS"
raise Exception("BIGBADABOOM")
Expand Down Expand Up @@ -209,15 +209,15 @@ def test_step_parameters_should_be_replaced_by_their_values(testdir):
from pytest_bdd import given, when, scenario, then, parsers
@given(parsers.parse('there are {start} cucumbers'), target_fixture="start_cucumbers")
def start_cucumbers(start):
def _(start):
return start
@when(parsers.parse('I eat {eat} cucumbers'))
def eat_cucumbers(start_cucumbers, eat):
def _(start_cucumbers, eat):
pass
@then(parsers.parse('I should have {left} cucumbers'))
def should_have_left_cucumbers(start_cucumbers, left):
def _(start_cucumbers, left):
pass
@scenario('test.feature', 'Scenario example 2')
Expand Down
Loading

0 comments on commit c0357d5

Please sign in to comment.