【发布时间】:2018-12-25 13:40:15
【问题描述】:
我有一个 Django 应用程序,我正在尝试使用 pytest 和 pytest-django 对其进行测试。但是,很多时候,当测试完成运行时,我会收到数据库无法删除的错误:DETAIL: There is 1 other session using the database.
基本上,我可以缩小到的最小测试代码是:
@pytest.fixture
def make_bundle():
a = MyUser.objects.create(key_id=uuid.uuid4())
return a
class TestThings:
def test_it(self, make_bundle):
all_users = list(MyUser.objects.all())
assert_that(all_users, has_length(1))
测试不时会因上述错误而失败。有什么我做错了吗?或者我该如何解决这个问题?
我使用的数据库是 PostgreSQL 9.6。
【问题讨论】:
-
是的,pytest-django 在对正在使用的生产数据库运行测试时不能很好地与 PostgreSQL 配合使用。 我一直在阅读 Two Scoops of Django 并试图弄清楚用于测试的最佳 PostgreSQL“影子数据库”设置,但我还没有。只是想让你知道我在同一条船上!
-
@ScottSkiles 问题是它不是生产数据库,它是一个正在为测试运行创建和删除的数据库。
标签: python django database pytest pytest-django