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:
- User sets
context.fixtures = ['foo.json'] in before_scenario()
- Our
setup_fixtures() reads and applies it to context.test.fixtures
- 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.
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:context.fixtures = ['foo.json']inbefore_scenario()setup_fixtures()reads and applies it tocontext.test.fixturescontext.fixturesso it doesn't carry overThis would require a one-line change in
setup_fixtures()after line 79:However, this could break existing code that:
context.fixturesinbefore_scenario()(like our test suite does)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.