小编use*_*381的帖子

RuntimeError: Database access not allowed, 使用“django_db”标记,或者“db”或“transactional_db”fixtures来启用它

我正在尝试运行 pytest 并收到此错误: RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

我在 test_models.py 中有以下测试,用于检查 uuid 是否已添加到用户(在自动添加中):

import pytest

from backendapps.core.models import User

pytestmark = pytest.mark.django_db

class TestUserModel():
    user = User.objects.create()

    assert user.uuid is not None
Run Code Online (Sandbox Code Playgroud)

我在根级别还有一个名为 conftest.py 的装置文件。

import pytest

from backendapps.core.models import Org, User

@pytest.fixture(autouse=True)
def enable_db_access(db):
    pass
Run Code Online (Sandbox Code Playgroud)

然后我也有这个 pytest.ini 也在根级别:

[pytest]
testpaths = backendapps
addopts = --ds=config.settings.local --reuse-db --create-db
python_files = tests.py test_*.py *_tests.py```
Run Code Online (Sandbox Code Playgroud)

我的测试数据库设置为:

        'TEST': {
            'NAME': …
Run Code Online (Sandbox Code Playgroud)

django pytest

5
推荐指数
1
解决办法
1810
查看次数

标签 统计

django ×1

pytest ×1