diff --git a/behave_django/environment.py b/behave_django/environment.py index 6c13c30..9520249 100644 --- a/behave_django/environment.py +++ b/behave_django/environment.py @@ -1,3 +1,4 @@ +import contextlib from copy import copy import django @@ -78,6 +79,10 @@ def setup_fixtures(self, context): else: context.test.fixtures = copy(fixtures) + # Auto-reset fixtures for next scenario to avoid carryover + with contextlib.suppress(AttributeError): + del context.fixtures + reset_sequences = getattr(context, 'reset_sequences', None) if django.VERSION >= (5, 2): context.test.__class__.reset_sequences = reset_sequences diff --git a/docs/fixtures.rst b/docs/fixtures.rst index 92b29cb..84a456d 100644 --- a/docs/fixtures.rst +++ b/docs/fixtures.rst @@ -29,10 +29,6 @@ If you wanted different fixtures for different scenarios: context.fixtures = ['user-data.json'] elif scenario.name == 'Check out cart': context.fixtures = ['user-data.json', 'store.json', 'cart.json'] - else: - # Resetting fixtures, otherwise previously set fixtures carry - # over to subsequent scenarios. - context.fixtures = [] You could also have fixtures per Feature too @@ -43,24 +39,23 @@ You could also have fixtures per Feature too context.fixtures = ['user-data.json'] # This works because behave will use the same context for # everything below Feature. (Scenarios, Outlines, Backgrounds) - else: - # Resetting fixtures, otherwise previously set fixtures carry - # over to subsequent features. - context.fixtures = [] -Of course, since ``context.fixtures`` is really just a list, you can mutate it -however you want, it will only be processed upon leaving the -``before_scenario()`` function of your ``environment.py`` file. Just keep in -mind that it does not reset between features or scenarios, unless explicitly -done so (as shown in the examples above). +``context.fixtures`` is processed after the ``before_scenario()`` function of +your ``environment.py`` file completes, and is automatically reset after each +scenario to prevent fixtures from carrying over to subsequent scenarios. .. attention:: Starting with **behave-django 2.0.0**, the ``context.fixtures`` attribute - will be automatically reset after each scenario. This means you will no - longer need to explicitly set ``context.fixtures = []`` to reset fixtures - between scenarios or features. The automatic reset will make fixture - handling more user-friendly and less error-prone. + is automatically reset after each scenario. You no longer need to + explicitly set ``context.fixtures = []`` to reset fixtures between + scenarios or features. + + If you are upgrading from an earlier version and were manually resetting + fixtures, you can safely remove those reset statements. If your code was + appending to ``context.fixtures`` in ``before_scenario()``, you will need + to update it to set the complete list of fixtures instead, since the + attribute is reset after each scenario. .. note:: diff --git a/tests/acceptance/environment.py b/tests/acceptance/environment.py index 6792c73..d220b61 100644 --- a/tests/acceptance/environment.py +++ b/tests/acceptance/environment.py @@ -4,10 +4,7 @@ def before_feature(context, feature): - if feature.name == 'Fixture loading': - context.fixtures = ['behave-fixtures.json'] - - elif feature.name == 'Fixture loading with decorator': + if feature.name == 'Fixture loading with decorator': # Including empty fixture to test that #92 is fixed context.fixtures = ['empty-fixture.json'] @@ -19,14 +16,20 @@ def before_feature(context, feature): def before_scenario(context, scenario): - if scenario.name == 'Load fixtures for this scenario and feature': - context.fixtures.append('behave-second-fixture.json') + # Set fixtures for each scenario explicitly since auto-reset happens after + # each scenario + if scenario.name == 'Load fixtures': + context.fixtures = ['behave-fixtures.json'] - if scenario.name == 'Load fixtures then reset sequences': - context.fixtures.append('behave-second-fixture.json') + elif scenario.name == 'Load fixtures for this scenario and feature': + context.fixtures = ['behave-fixtures.json', 'behave-second-fixture.json'] + + elif scenario.name == 'Load fixtures then reset sequences': + context.fixtures = ['behave-fixtures.json', 'behave-second-fixture.json'] context.reset_sequences = True - if scenario.name == 'Load fixtures with databases option': + elif scenario.name == 'Load fixtures with databases option': + context.fixtures = ['behave-fixtures.json'] context.databases = '__all__'