mixxorz / behave-django

Behave BDD integration for Django
https://pythonhosted.org/behave-django/
38 stars 5 forks source link

Adding fixtures to your behave tests #5

Closed mixxorz closed 9 years ago

mixxorz commented 9 years ago

Since we're using Django's StaticLiveServerTestCase internally to set everything up, we should have the ability to load our fixtures. Here's my proposal on how this will work.

In environment.py, before the call to behave-django's environment.before_scenario(), we can load our context with the fixtures array.

def before_scenario(context, scenario):
    context.fixtures = ['user-data.json']
    environment.before_scenario(context, scenario)

behave-django would then pass this on to the test case and your fixture will be loaded.

If you wanted different fixtures for different scenarios:

def before_scenario(context, scenario):
    if scenario.name == 'User login with valid credentials':
        context.fixtures = ['user-data.json']
    elif scenario.name == 'Check out cart':
        context.fixtures = ['user-data.json', 'store.json', 'cart.json']
    environment.before_scenario(context, scenario)

You could also have fixtures per Feature too

def before_feature(context, feature):
    if feature.name == 'Login':
        context.fixtures = ['user-data.json']
        # This works because behave will use the same context for everything below Feature. (Scenarios, Outlines, Backgrounds)

def before_scenario(context, scenario):
    # You wouldn't need to change anything
    environment.before_scenario(context, scenario)

Does this look good? Does anyone have any other suggestions?

ivanistheone commented 9 years ago

Looks good. I'd use that.