【发布时间】:2022-06-13 12:54:56
【问题描述】:
我正在使用 pytest,但我在下面的夹具中遇到了数据库访问问题。我到处都有 django_db 标记。
[test_helpers.py]
import pytest
from django.test import Client
from weblab.middleware.localusermiddleware import _set_current_user
@pytest.fixture(scope="class")
@pytest.mark.django_db
def class_test_set_up(request):
request.cls.client = Client()
username = "username"
user = User.objects.get(username=username)
_set_current_user(user)
我得到
RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
在线user = User.objects.get(username=username)
[test_tmp_fixture.py]
import pytest
from tests.factories.sample.test_factories import TestFactory
from tests.tests_helpers.test_helpers import class_test_set_up
SIZE = 5
@pytest.mark.django_db
@pytest.fixture(scope="class")
def set_up_objs(request):
request.cls.factory = TestFactory
request.instance.objs = request.cls.factory.create_batch(SIZE)
@pytest.mark.django_db
@pytest.mark.usefixtures("class_test_set_up", "set_up_objs")
class TestTest:
@pytest.mark.django_db
def test_test(self):
print("Hello Pytest")
我的设置是带有插件的 pytest-7.0.1:lazy-fixture-0.6.3、Faker-13.3.2、django-4.5.2 和 django 版本 3.2.12
在回溯控制台中显示/pytest_lazyfixture.py:39: 的问题
【问题讨论】:
标签: python django pytest fixtures