def _django_db_fixture_helper(transactional, request, _django_cursor_wrapper):
if is_django_unittest(request):
return
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)
else:
if 'live_server' in request.funcargnames:
return
from django.test import TestCase
_django_cursor_wrapper.enable()
_django_cursor_wrapper._is_transactional = False
case = TestCase(methodName='__init__')
case._pre_setup()
request.addfinalizer(_django_cursor_wrapper.disable)
request.addfinalizer(case._post_teardown)
正如我看到你使用 pytest-django (这很好)从它的这段代码中,如果它是非事务性数据库,它不会刷新数据库。因此,在您的“其他”测试中,您必须使用 transactional_db,然后它将根据您的需要被隔离。
所以你的代码看起来像:
class TestCase:
def test_some_unittest(transactional_db):
# Create some items
#...
def test_with_selenium(browser):
# The items from the above testcase does not exist in this testcase
然而,对 pytest-django 的改进可能是在fixture 值的yield 之前而不是之后执行刷新,这更有意义。拆解中的内容并不重要,重要的是设置是否正确。作为一个建议,对于浏览器夹具,您可以使用 pytest-splinter 插件