pytest-dev / pytest-bdd

BDD library for the pytest runner
https://pytest-bdd.readthedocs.io/en/latest/
MIT License
1.31k stars 221 forks source link

Multiple 'given's using same target_fixture #513

Closed ShedPlant closed 2 years ago

ShedPlant commented 2 years ago

https://pypi.org/project/pytest-bdd/

The documentation suggests that you can use multiple 'given' texts with a single target fixture:

@given("I have an article")
@given("there's an article")
def article(author, target_fixture="article"):
    return create_test_article(author=author)

Originally I had a single 'given' text and it worked fine:

@given("the user creates a vehicle", target_fixture="vehicle_in_dynamo_f")
def vehicle_in_dynamo(users_vehicles_dynamo_table, example_user_vehicle, target_fixture="vehicle_in_dynamo_f"):
    users_vehicles_dynamo_table.put_item(Item=example_user_vehicle)

When I added a second 'given' according to documentation:

@given("the user creates a vehicle")
@given("the user has a vehicle")
def vehicle_in_dynamo(users_vehicles_dynamo_table, example_user_vehicle, , target_fixture="vehicle_in_dynamo_f"):
    users_vehicles_dynamo_table.put_item(Item=example_user_vehicle)

I see an error (among many)

E fixture 'target_fixture' not found

I also tried this but it doesn't work either:

@given("the user creates a vehicle", target_fixture="vehicle_in_dynamo_f")
@given("the user has a vehicle", target_fixture="vehicle_in_dynamo_f")
def vehicle_in_dynamo(users_vehicles_dynamo_table, example_user_vehicle):
    users_vehicles_dynamo_table.put_item(Item=example_user_vehicle)

It does seem a bit odd that target_fixture argument moves out from the 'given' decorator to the decorated function.

Can someone please tell me if I'm doing it wrong and/or update the documentation?

elchupanebrej commented 2 years ago

You have missed return from step function

@given("the user creates a vehicle", target_fixture="vehicle_in_dynamo_f")
@given("the user has a vehicle", target_fixture="vehicle_in_dynamo_f")
def vehicle_in_dynamo(users_vehicles_dynamo_table, example_user_vehicle):
    return users_vehicles_dynamo_table.put_item(Item=example_user_vehicle)
ShedPlant commented 2 years ago

Maybe that was it, thanks 😊