elaborate that step arguments are normal fixtures. #104

This commit is contained in:
Anatoly Bubenkov 2015-03-16 23:48:15 +01:00
parent ce654199f4
commit c4b2e7fe92
2 changed files with 14 additions and 6 deletions

View File

@ -235,7 +235,6 @@ arguments after the parser.
You can implement your own step parser. It's interface is quite simple. The code can looks like:
.. code-block:: python
import re
@ -266,6 +265,15 @@ You can implement your own step parser. It's interface is quite simple. The code
def start_cucumbers(start):
return dict(start=start, eat=0)
Step arguments are fixtures as well!
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Step arguments are injected into pytest `request` context as normal fixtures with the names equal to the names of the
arguments. This opens a number of possibilies:
* you can access step's argument as a fixture in other step function just by mentioning it as an argument (just like any othe pytest fixture)
* if the name of the step argument clashes with existing fixture, it will be overridden by step's argument value; this way you can set/override the value for some fixture deeply inside of the fixture tree in a ad-hoc way by just choosing the proper name for the step argument.
Multiline steps
---------------
@ -309,7 +317,7 @@ step arguments and capture lines after first line (or some subset of them) into
pass
@given(re.compile(r'I have a step with:\n(?P<text>.+)', re.DOTALL))
@given(parsers.parse('I have a step with:\n{text}'))
def i_have_text(text):
return text
@ -318,8 +326,9 @@ step arguments and capture lines after first line (or some subset of them) into
def text_should_be_correct(i_have_text, text):
assert i_have_text == text == 'Some\nExtra\nLines'
Pay attention to the re.DOTALL option used for step registration. When used, .+ will also capture newlines.
Note that `then` step definition (`text_should_be_correct`) in this example uses `text` fixture which is provided
by a a `given` step (`i_have_text`) argument with the same name (`text`). This possibility is described in
the `Step arguments are fixtures as well!`_ section.
Scenario parameters
-------------------

View File

@ -1,5 +1,4 @@
"""Multiline steps tests."""
import re
import textwrap
import pytest
@ -77,7 +76,7 @@ def test_multiline(request, tmpdir, feature_text, expected_text):
test_multiline(request)
@given(parsers.re(r'I have a step with:\n(?P<text>.+)', flags=re.DOTALL))
@given(parsers.parse('I have a step with:\n{text}'))
def i_have_text(text):
return text