Auto-reset context.fixtures after each scenario
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.fixturesafter processing insetup_fixtures()usingcontextlib.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
contextobject across scenarios and features. Any attribute set oncontext(likecontext.fixtures) persists unless explicitly cleared.To make this more user-friendly, we could automatically clear
context.fixturesaftersetup_fixtures()processes it. This would mean:
- User sets
context.fixtures = ['foo.json']inbefore_scenario()- Our
setup_fixtures()reads and applies it tocontext.test.fixtures- We clear
context.fixturesso it doesn't carry overThis 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 scenarioHowever, this could break existing code that:
- Appends to
context.fixturesinbefore_scenario()(like our test suite does)- Expects to read
context.fixturesafter it's been setWhen 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.