Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions behave_django/environment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
from copy import copy

import django
Expand Down Expand Up @@ -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
Expand Down
29 changes: 12 additions & 17 deletions docs/fixtures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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::

Expand Down
21 changes: 12 additions & 9 deletions tests/acceptance/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand All @@ -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__'


Expand Down