Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 15, 2025

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)


💡 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 AI and others added 2 commits November 15, 2025 17:15
Co-authored-by: bittner <665072+bittner@users.noreply.github.com>
Co-authored-by: bittner <665072+bittner@users.noreply.github.com>
Copilot AI changed the title [WIP] Automatically reset context.features after setup Auto-reset context.fixtures after each scenario Nov 15, 2025
Copilot AI requested a review from bittner November 15, 2025 17:20
@bittner bittner marked this pull request as ready for review November 18, 2025 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automatically reset context.features

2 participants