简体   繁体   English

即使有标记,pytest-django 也不允许访问数据库

[英]pytest-django won't allow database access even with mark

I'm having a difficult time figuring out what is wrong with my setup.我很难弄清楚我的设置有什么问题。 I'm trying to test a login view, and no matter what I try, I keep getting:我正在尝试测试登录视图,无论我尝试什么,我都会收到:

Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

My test:我的测试:

import pytest

from ..models import User


@pytest.mark.django_db
def test_login(client):
    # If an anonymous user accesses the login page:
    response = client.get('/users/login/')
    # Then the server should respond with a successful status code:
    assert response.status_code == 200

    # Given an existing user:
    user = User.objects.get(username='user')
    # If we attempt to log into the login page:
    response = client.post('/users/login/', {'username': user.username, 'password': 'somepass'})
    # Then the server should redirect the user to the default redirect location:
    assert response.status_code == 302

My conftest.py file, in the same tests directory:我的 conftest.py 文件,在同一个测试目录中:

import pytest

from django.core.management import call_command


@pytest.fixture(autouse=True)
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', 'test_users.json')

My pytest.ini file (which specifies the correct Django settings file):我的 pytest.ini 文件(它指定了正确的 Django 设置文件):

[pytest]
DJANGO_SETTINGS_MODULE = config.settings

I'm stumped.我难住了。 I've tried using scope="session" like in the documentation together with either an @pytest.mark.django_db mark, a db fixture (as a parameter to the test function), or both with no luck.我试过在文档中使用scope="session"以及@pytest.mark.django_db标记、 db fixture(作为测试函数的参数),或者两者都没有运气。 I've commented out each line of the test to figure out which one was triggering the problem, but couldn't figure it out.我已经注释掉了测试的每一行,以确定哪一行触发了问题,但无法弄清楚。 I could only get the test to run at all if I removed all db-dependent fixtures/marks/code from the test and had a simple assert True .如果我从测试中删除所有依赖于数据库的装置/标记/代码并有一个简单的assert True我只能让测试运行。 I don't believe the issue is in my Django settings, as the development server runs fine and is able to access the database.我不相信问题出在我的 Django 设置中,因为开发服务器运行良好并且能够访问数据库。

What am I missing here?我在这里缺少什么?

Apparently this is a case of "deceiving exception syndrome".显然,这是一个“欺骗异常综合症”的案例。 I had a migration that created groups with permissions, and since tests run all migrations at once, the post-migrate signal that creates the permissions that migration depends on was never run before getting to that migration.我有一个创建具有权限的组的迁移,并且由于测试一次运行所有迁移,因此在进行迁移之前从未运行创建迁移所依赖的权限的迁移后信号。

It seems that if there is any database-related error before the actual tests start running, this exception is raised, which makes it very difficult to debug exactly what is going wrong.似乎如果在实际测试开始运行之前有任何与数据库相关的错误,就会引发此异常,这使得很难准确地调试出问题所在。 I ended up updating my migration script to manually cause permissions to be created so that the migration could run, and the error went away.我最终更新了我的迁移脚本以手动创建权限,以便迁移可以运行,并且错误消失了。

You can add below code in your conftest.py as per the official documentation to allow DB access without django_db marker.您可以根据官方文档在conftest.py添加以下代码,以允许在没有django_db标记的情况下访问数据库。

@pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(db):
    pass

Ref: https://pytest-django.readthedocs.io/en/latest/faq.html#how-can-i-give-database-access-to-all-my-tests-without-the-django-db-marker参考: https : //pytest-django.readthedocs.io/en/latest/faq.html#how-can-i-give-database-access-to-all-my-tests-without-the-django-db-标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM