【问题标题】:Celery_worker fixture in pytest, connection already closed errorpytest 中的 Celery_worker fixture,连接已关闭错误
【发布时间】:2023-02-20 15:53:36
【问题描述】:
我有这样的pytest结构
import pytest
@pytest.mark.django_db
class TestClass:
def test_celery_mht_notification_create(self, celery_worker, user):
# some test logic
当我使用celery_worker fixture 时,出现这样的错误psycopg2.InterfaceError: connection already closed
如何解决?
【问题讨论】:
标签:
python
django
celery
pytest
【解决方案1】:
根据the solution,他们在pytest-django 上的issue created 中给了你:
另一方面,对我来说,一个解决方法是作为事务测试运行
@pytest.mark.django_db(transaction=True)
在第一个链接的 cmets 线程中进行更多挖掘,提出了恕我直言 a cleaner solution,以及对它发生原因的解释:
如果您正在使用例如Py.test 并想使用进程内工作者,你可以做类似的事情
def pytest_configure():
from celery.fixups.django import DjangoWorkerFixup
DjangoWorkerFixup.install = lambda x: None
禁用工作人员修正,这在这种情况下是不必要的。