-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Q: why '@when' doesn't export target_fixture? #402
Comments
|
I've just struggled with the same issue. Since there doesn't seem to be an argument against an implementation, at least I would be very thankful for a MR @bulletRush ❤️ |
If you need the result of your step to be used somewhere else, then your step should probably be a "given". Although I wouldn't recommend it, if you really need to store the result of a step and reuse it somewhere else, you can always use a fixture like @pytest.fixture
def context():
return {} and use its content in the "when" and "then" steps: @when("something happens")
def when_something_happens(context):
context['something_happened'] = True
@then("something should have happened")
def then_something_has_happened(context):
assert context['something_happened'] is True A PR that allows "when" steps to have a target_fixture parameter would probably be rejected unless there is a very good use case and motivation for it. |
@youtux the issue here is that pytest-bdd assumes you are not testing the return values of functions. The action in the Given I want to search for cake How would you test this? You need a way to cleanly pass the response from Otherwise, people will be forced to skip |
@pytest.fixture
def context():
return {}
@given("I want to search for cake")
def i_want_to_search_for_cake(...):
...
@when(parsers.parse("I send a GET request to {url} with cake as the payload"))
def i_send_request_with_cacke_as_payload(context, http_client, method, url):
context["response"] = http_client.get(url, {"cake": "a cake"})
@then("I get a response with the corresponding HTML of the page")
def i_get_a_response_with_html(context):
html = parse_html(context["response"].content)
assert html.title = "a cake"
assert ... |
This is just a dirty workaround. Users can then just put anything inside of context. Also the name context isn't very useful. I don't see why you're against such a change. There is a genuine use case and instead of supporting it you just want everyone to use a workaround. :( |
Indeed this is a use case, maybe the result of a when/then step can be injected as a fixture in the context, so that assertions can refer to it more naturally. @olegpidsadnyi what do you think about allowing when/then steps to have a target_fixture param to store the result of the step? Maybe one more motivation for it is that you could have "then" statements that can assert on an object that is created by the setup, or as a result of a user interaction. For example: Feature: Blog
Scenario: Publishing the article
Given there is an article
When I go to the article page
And I press the publish button
Then the article should be published
Scenario: Creating the article
When I go to the create article page
And I press the create button
Then the article should not be published and the steps could be defined like from pytest_bdd import scenario, given, when, then
scenarios(...)
@given("there is an article", target_fixture="article")
def there_is_an_article():
return create_test_article()
...
@then("there should be a new article")
def there_should_be_a_new_article(session, target_fixture="article"): # <-- Important bit here
article session.query(Article).order_by(Article.id.desc()).first()
assert article is not None
return article
@then(parsers.re(r"the article should (?P<negation>not )be published")
def article_is_published(article, negation): # <-- this article comes either form the Given step or the Then step
expected = not negation
assert article.is_published is expected |
@olegpidsadnyi please tell me you agree with me |
I am a newbie of
pytest-bdd
. I have a question aboutwhen
decorator.If we export 'target_fixture' args for 'when' decorator, we can rewrite
to
for real project test cases, this will make more easy for reusing those steps.
so why
pytest-bdd
does not exporttarget_fixture
for when decorator? should I make a MR for this change?The text was updated successfully, but these errors were encountered: