在 Django 应用程序的测试运行中使用 pytest/pytest-django 时如何将数据保存到数据库?
我使用
py.test --nomigrations --reuse-db -s
运行 pytest,并且 Postgres DB test_<configured_db_name>
按预期创建,但是在测试之间似乎没有任何内容被持久化到数据库,并且在测试运行结束时数据库为空。
import pytest
from django.contrib.auth.models import User
@pytest.mark.django_db(transaction=False)
def test_insert_user():
user = User.objects.create_user(username="test_user", email="[email protected]", password="test")
users = User.objects.all()
assert len(users) > 0
@pytest.mark.django_db(transaction=False)
def test_check_user():
users = User.objects.all()
assert len(users) > 0
第一个测试通过了,第二个测试并没有让我怀疑是否有任何东西被持久化到数据库。根据 pytest-django 文档
@pytest.mark.django_db(transaction=False)
不会回滚受装饰测试影响的任何内容。
谢谢你,
/大卫
为每个函数预填充数据库的另一种方法是这样的:
import pytest
from django.contrib.auth.models import User
@pytest.fixture(scope='module')
def django_db_setup(django_db_setup, django_db_blocker):
print('setup')
with django_db_blocker.unblock():
User.objects.create(username='a')
assert set(u.username for u in User.objects.all()) == {'a'}
@pytest.mark.django_db
def test1():
print('test1')
User.objects.create(username='b')
assert set(u.username for u in User.objects.all()) == {'a', 'b'}
@pytest.mark.django_db
def test2():
print('test2')
User.objects.create(username='c')
assert set(u.username for u in User.objects.all()) == {'a', 'c'}
这个方法的好处是 setup 函数只调用一次:
plugins: django-3.1.2
collected 2 items
mytest.py setup
test1
.test2
.
=================== 2 passed in 1.38 seconds ====================
不好的是,对于这样一个简单的测试来说,1.38 秒有点太多了。
--reuse-db
是一种更快的方法。
我已经解决了这个问题——为每个函数预填充数据库——通过定义一个范围为
function
的固定装置(即 model
和 session
不起作用)。
这是在 Django 中测试视图的代码。
# This is used to fill the database more easily
from mixer.backend.django import mixer
import pytest
from django.test import RequestFactory
from inventory import views
from inventory import services
pytestmark = pytest.mark.django_db
@pytest.fixture(scope="function")
def fill_db():
""" Just filling the DB with my data """
for elem in services.Search().get_lookup_data():
mixer.blend('inventory.Enumeration', **elem)
def test_grid_anonymous(fill_db):
request = RequestFactory().get('/grid/')
response = views.items_grid(request)
assert response.status_code == 200, \
"Should be callable by anyone"
def test_list_anonymous(fill_db):
request = RequestFactory().get('/')
response = views.items_list(request)
assert response.status_code == 200, \
"Should be callable by anyone"