【问题标题】:Pytest errors connecting to test database连接到测试数据库的 Pytest 错误
【发布时间】:2021-05-17 12:38:01
【问题描述】:

我遇到了在运行 pytest 之前从未见过的奇怪错误,我正在运行我的测试,几乎所有错误都由于不允许访问数据库而出错。

这是上周未发生的新错误,因此不是本地代码更改,但我不确定到底发生了什么。

谁能指出我正确的方向?

这是错误:

conftest.py:52: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/local/lib/python3.8/site-packages/django/db/models/query.py:287: in __iter__
    self._fetch_all()
/usr/local/lib/python3.8/site-packages/cacheops/query.py:271: in _fetch_all
    return self._no_monkey._fetch_all(self)
/usr/local/lib/python3.8/site-packages/django/db/models/query.py:1308: in _fetch_all
    self._result_cache = list(self._iterable_class(self))
/usr/local/lib/python3.8/site-packages/django/db/models/query.py:53: in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
/usr/local/lib/python3.8/site-packages/django/db/models/sql/compiler.py:1154: in execute_sql
    cursor = self.connection.cursor()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <django.test.testcases._DatabaseFailure object at 0x7fb361b2d2b0>

    def __call__(self):
>       raise AssertionError(self.message)
E       AssertionError: Database queries to 'test' are not allowed in this test. Add 'test' to pytest_django.fixtures._django_db_fixture_helper.<locals>.PytestDjangoTestCase.databases to ensure proper test isolation and silence this failure.

/usr/local/lib/python3.8/site-packages/django/test/testcases.py:146: AssertionError
_ ERROR at setup of TestAccountViews.test_can_edit_account_info_with_backticks[admin] _

db = None

    @pytest.fixture
    def slug(db):
        # delete these guys because there should only ever be 1 of each
        from apps.payments.models.ledger_models import TransactionCode
>       for code_object in TransactionCode.objects.all():

【问题讨论】:

    标签: python django pytest pytest-django


    【解决方案1】:

    看来这是版本控制问题。

    pytest-djangohttps://pypi.org/project/pytest-django/4.3.0/ 几天前更新,显然这是一个新错误。我在他们的更新日志中没有看到任何内容:https://pytest-django.readthedocs.io/en/latest/changelog.html,但从 4.3.0 版本降到 4.2.0 版本确实解决了这个问题。

    另一个解决该问题的方法是更改​​ pytest_sessionstart() 以允许访问 conftest.py 中的所有数据库,如下所示:

    def pytest_sessionstart(session):
        from django.test import TestCase
        TestCase.multi_db = True
        TestCase.databases = '__all__'      # here
    

    【讨论】:

    • 确保锁定您的依赖项,并且不会在您背后发生破坏性更新...
    • @AKX 是的,我拥有它用于其他一切,只是认为它对测试无关紧要。已在要求中对此进行了更改。
    【解决方案2】:

    根据pytest-django 文档中的Tests requiring multiple databases,现在可能(这对我在 pytest-django 版本 4.3.0 上有效)通过使用以下 pytest 标记来解决此问题:

    @pytest.mark.django_db(databases=['default', 'test'])
    def test_my_code():
        ...
    

    传递给databases= 的列表是您在settings.DATABASES 中定义的数据库键的列表。

    然而,这个领域似乎会发生一些变化,可能只是部分实施,所以 YMMV。

    【讨论】:

    • 我不想使用默认数据库,我想使用不同的数据库来测试我该怎么做?
    • 传递给databases= 的数据库列表是您的django settings.py 文件中DATABASES 字典中的键列表,因此您应该只传递要使用的数据库的键...如果您想为所有测试使用不同的数据库,那么最简单的方法是在测试时将DATABASES['default'] 配置为不同,您可以通过查看sys.argv 或使用环境变量来检测
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 2021-05-13
    • 2021-07-01
    • 2018-10-01
    • 2020-12-10
    • 2013-05-07
    相关资源
    最近更新 更多