From de8d6fe70ac145bfd3a950564a5472ca6e5e0c5b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 15 Nov 2025 17:09:19 +0000 Subject: [PATCH 1/3] Initial plan From 94e8c4846fa773988fefe8459146e420b62945f3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 15 Nov 2025 17:15:36 +0000 Subject: [PATCH 2/3] Implement automatic reset of context.fixtures after each scenario Co-authored-by: bittner <665072+bittner@users.noreply.github.com> --- behave_django/environment.py | 7 +++++++ docs/fixtures.rst | 29 ++++++++++++----------------- tests/acceptance/environment.py | 20 +++++++++++--------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/behave_django/environment.py b/behave_django/environment.py index 6c13c30..93d512a 100644 --- a/behave_django/environment.py +++ b/behave_django/environment.py @@ -78,6 +78,13 @@ def setup_fixtures(self, context): else: context.test.fixtures = copy(fixtures) + # Auto-reset fixtures for next scenario to avoid carryover + try: + del context.fixtures + except AttributeError: + # Attribute not set at current level, ignore + pass + 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..434b508 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,19 @@ 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__' From a340144423f776819e5a64d635e0ca348e7da1e7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 15 Nov 2025 17:16:56 +0000 Subject: [PATCH 3/3] Fix linting issues (use contextlib.suppress and line length) Co-authored-by: bittner <665072+bittner@users.noreply.github.com> --- behave_django/environment.py | 6 ++---- tests/acceptance/environment.py | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/behave_django/environment.py b/behave_django/environment.py index 93d512a..9520249 100644 --- a/behave_django/environment.py +++ b/behave_django/environment.py @@ -1,3 +1,4 @@ +import contextlib from copy import copy import django @@ -79,11 +80,8 @@ def setup_fixtures(self, context): context.test.fixtures = copy(fixtures) # Auto-reset fixtures for next scenario to avoid carryover - try: + with contextlib.suppress(AttributeError): del context.fixtures - except AttributeError: - # Attribute not set at current level, ignore - pass reset_sequences = getattr(context, 'reset_sequences', None) if django.VERSION >= (5, 2): diff --git a/tests/acceptance/environment.py b/tests/acceptance/environment.py index 434b508..d220b61 100644 --- a/tests/acceptance/environment.py +++ b/tests/acceptance/environment.py @@ -16,7 +16,8 @@ def before_feature(context, feature): def before_scenario(context, scenario): - # Set fixtures for each scenario explicitly since auto-reset happens after each scenario + # Set fixtures for each scenario explicitly since auto-reset happens after + # each scenario if scenario.name == 'Load fixtures': context.fixtures = ['behave-fixtures.json']