behave-django icon indicating copy to clipboard operation
behave-django copied to clipboard

Auto-reset context.fixtures after each scenario

Open Copilot opened this issue 4 months ago • 0 comments

context.fixtures now auto-resets after each scenario, eliminating the need for manual cleanup and reducing user error.

Changes

Core implementation (behave_django/environment.py):

  • Delete context.fixtures after processing in setup_fixtures() using contextlib.suppress(AttributeError) to handle cross-level attribute deletion

Test adaptation (tests/acceptance/environment.py):

  • Replace .append() pattern with complete fixture list assignment per scenario

Documentation (docs/fixtures.rst):

  • Remove manual reset examples and add migration guidance for users who append to context.fixtures

Migration

Before:

def before_feature(context, feature):
    context.fixtures = ['base.json']

def before_scenario(context, scenario):
    if scenario.name == 'Special case':
        context.fixtures.append('extra.json')  # Won't work - fixtures auto-reset

After:

def before_scenario(context, scenario):
    if scenario.name == 'Special case':
        context.fixtures = ['base.json', 'extra.json']  # Set complete list
    else:
        context.fixtures = ['base.json']

Breaking Changes

Code that appends to context.fixtures in before_scenario() must be updated to set the complete fixture list, as the attribute is deleted after each scenario.

Original prompt

This section details on the original issue you should resolve

<issue_title>Automatically reset context.features</issue_title> <issue_description>Currently, when a fixture is set for a scenario, it must be reset for all the other scenarios, as described in the documentation. This is not particularly user-friendly and leaves room for error.

The reason fixtures need manual resetting is that behave reuses the same context object across scenarios and features. Any attribute set on context (like context.fixtures) persists unless explicitly cleared.

To make this more user-friendly, we could automatically clear context.fixtures after setup_fixtures() processes it. This would mean:

  1. User sets context.fixtures = ['foo.json'] in before_scenario()
  2. Our setup_fixtures() reads and applies it to context.test.fixtures
  3. We clear context.fixtures so it doesn't carry over

This would require a one-line change in setup_fixtures() after line 79:

fixtures = getattr(context, 'fixtures', [])
if django.VERSION >= (5, 2):
    context.test.__class__.fixtures = copy(fixtures)
else:
    context.test.fixtures = copy(fixtures)
del context.fixtures  # Auto-reset for next scenario

However, this could break existing code that:

  • Appends to context.fixtures in before_scenario() (like our test suite does)
  • Expects to read context.fixtures after it's been set

When this feature is added, we should issue a major release, because it will be a small but breaking change.</issue_description>

<agent_instructions>Update existing tests (if necessary), make the suggested change and align the documentation accordingly. Also update the "attention" note on automatic reset of the fixtures attribute.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

  • Fixes behave/behave-django#179

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Nov 15 '25 17:11 Copilot