Test Isolation

Database transactions per scenario

Each scenario is run inside a database transaction, just like Django does it with the related TestCase implementation. So you can do something like:

@given('user "{username}" exists')
def create_user(context, username):
    # This won't be here for the next scenario
    User.objects.create_user(username=username, password='correcthorsebatterystaple')

And you don’t have to clean the database yourself.

Troubleshooting

Note

Users have reported that test isolation and loading fixtures works differently with different test runners (e.g. when the --simple option is used). This is likely related to the Django TestCase class configured for the runner.

Be sure you understand which Django TestCase class is used in your case, and how it’s implemented in Django. See the runner module and Django’s documentation on Writing and running tests for details.