Skip to content

Use setup/teardown from Django (TransactionTestCase) #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,44 @@ def _django_db_fixture_helper(transactional, request, _django_cursor_wrapper):
if is_django_unittest(request):
return

if not transactional and 'live_server' in request.funcargnames:
# Do nothing, we get called with transactional=True, too.
return

django_case = None

_django_cursor_wrapper.enable()
request.addfinalizer(_django_cursor_wrapper.disable)

if transactional:
_django_cursor_wrapper.enable()

def flushdb():
"""Flush the database and close database connections"""
# Django does this by default *before* each test
# instead of after.
from django.db import connections
from django.core.management import call_command

for db in connections:
call_command('flush', verbosity=0,
interactive=False, database=db)
for conn in connections.all():
conn.close()

request.addfinalizer(_django_cursor_wrapper.disable)
request.addfinalizer(flushdb)
from django import get_version

if get_version() >= '1.5':
from django.test import TransactionTestCase as django_case

else:
# Django before 1.5 flushed the DB during setUp.
# Use pytest-django's old behavior with it.
def flushdb():
"""Flush the database and close database connections"""
# Django does this by default *before* each test
# instead of after.
from django.db import connections
from django.core.management import call_command

for db in connections:
call_command('flush', verbosity=0,
interactive=False, database=db)
for conn in connections.all():
conn.close()
request.addfinalizer(flushdb)

else:
from django.test import TestCase
from django.test import TestCase as django_case

_django_cursor_wrapper.enable()
_django_cursor_wrapper._is_transactional = False
case = TestCase(methodName='__init__')
if django_case:
case = django_case(methodName='__init__')
case._pre_setup()
request.addfinalizer(_django_cursor_wrapper.disable)
request.addfinalizer(case._post_teardown)


Expand Down