这是对 pytest.mark.django_db 的无效使用吗?

is this an invalid use of pytest.mark.django_db?

对于此示例,UserProfile 模型中的所有内容都是可选的,但用户模型的用户外键除外

@pytest.mark.django_db
def create_userprofile_list():
    full_permissions_user, _ = get_user_model().objects.get_or_create(
        username="admin_testuser",
        email="admin_testuser@user.com",
        is_superuser=True,
        is_staff=True,
    )
    staff_permissions_user, _ = get_user_model().objects.get_or_create(
        username="staff_testuser",
        email="staff_testuser@user.com",
        is_superuser=False,
        is_staff=True,
    )
    user_permissions, _ = get_user_model().objects.get_or_create(
        username="normal_testuser",
        email="normal_testuser@user.com",
        is_superuser=False,
        is_staff=False,
    )
    user_list = [full_permissions_user,
                 staff_permissions_user,
                 user_permissions]
    return [UserProfile.objects.get_or_create(user=user)[0] for user in user_list]

userprofile_list = create_userprofile_list()

我会在其他测试中使用 userprofile_list,但我无法让它与测试数据库一起使用。

错误如下:

userprofile/tests/test_userprofiles.py:None (userprofile/tests/test_userprofiles.py)
userprofile/tests/test_userprofiles.py:39: in <module>
    userprofile_list = create_userprofile_list()
userprofile/tests/test_userprofiles.py:15: in create_userprofile_list
    full_permissions_user, _ = get_user_model().objects.get_or_create(
/usr/local/lib/python3.9/site-packages/django/db/models/manager.py:85: in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
/usr/local/lib/python3.9/site-packages/django/db/models/query.py:573: in get_or_create
    return self.get(**kwargs), False

...

/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py:26: in inner
    return func(*args, **kwargs)
/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py:259: in cursor
    return self._cursor()
/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py:235: in _cursor
    self.ensure_connection()
E   RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

如您所知,我将对这些用户进行 运行 网站测试。这是执行此操作的好方法吗?这是一种很好地使用 pytest.mark.django_db 装饰器的方法吗?

将其设为 fixture 并在使用它的测试中使用 django_db 标记。

@pytest.fixture
def userprofile_list():
    full_permissions_user, _ = get_user_model().objects.get_or_create(
        username="admin_testuser",
        email="admin_testuser@user.com",
        is_superuser=True,
        is_staff=True,
    )
    staff_permissions_user, _ = get_user_model().objects.get_or_create(
        username="staff_testuser",
        email="staff_testuser@user.com",
        is_superuser=False,
        is_staff=True,
    )
    user_permissions, _ = get_user_model().objects.get_or_create(
        username="normal_testuser",
        email="normal_testuser@user.com",
        is_superuser=False,
        is_staff=False,
    )
    user_list = [full_permissions_user,
                 staff_permissions_user,
                 user_permissions]
    return [UserProfile.objects.get_or_create(user=user)[0] for user in user_list]

@pytest.mark.django_db
def test_something(userprofile_list):
    ...