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

doc: multiline-steps: code snippet wrong? #104

Closed blueyed closed 9 years ago

blueyed commented 9 years ago

The documentation on https://github.com/pytest-dev/pytest-bdd#multiline-steps (https://github.com/pytest-dev/pytest-bdd/blame/master/README.rst#L317-L321) seems to be wrong:

@then('the text should be parsed with correct indentation')
def eat_cucumbers(i_have_text, text):
    assert i_have_text == text == """Some
Extra
Lines"""

I cannot see where the "Some\nExtra\nLines" is coming from. Probably the assertion needs to be fixed / enhanced to handle / compare the indentation.

Also, while at it, the function should be renamed. It's not about cucumbers in this section.. :)

bubenkoff commented 9 years ago
I cannot see where the "Some\nExtra\nLines" is coming from. Probably the 
assertion needs to be fixed / enhanced to handle / compare the indentation.

it comes from the given, because like any given it's fixture which you can access later in other given, when, then functions

bubenkoff commented 9 years ago

about the rename i totally agree and will do

blueyed commented 9 years ago

it comes from the given

Ah, I see. Missed that. But then the text argument is invalid here maybe? I see that i_have_text comes from the when, but not where text is coming from.

bubenkoff commented 9 years ago

text is coming from the step text as a step argument It's probably a bit too complicated example

bubenkoff commented 9 years ago
@given(re.compile(r'I have a step with:\n(?P<text>.+)', re.DOTALL))
def i_have_text(text):
    return text

this is an argumented step so it's function gets an argument 'text' because it's a regex group name cool that you've pointed out this place - because it uses an outdated way of declaring argumented steps

blueyed commented 9 years ago

So this means that any step arguments are available in the then automatically / when requested?

I would have expected that only fixtures (and pytest-bdd's extension of them) could be requested/used as arguments.

Anyway, it would certainly benefit from some explanation.. :)

bubenkoff commented 9 years ago

any step argument is injected into pytest's request as a normal fixture, no difference, so you access it in natural way. this allows you to 'mark.parametrize' your test with argumented given, without having to decorate the actual test! And if the name of the argument clashes with existing fixture name, it will override it - so very powerful and we use that quite a lot. pytest-bdd is build on top of fixtures and only fixtures :) yes, i'll add more to the step arguments section - about that fact that step arguments are fixtures as well.

blueyed commented 9 years ago

Thanks!

Sounds very powerful indeed.