在django 2.2中,此测试中不允许对“缓存”进行数据库查询

时间:2019-10-09 17:33:40

标签: django django-2.2

我正在将项目从django 1.11迁移到django 2.2。 我们具有基于数据库的缓存,该缓存可在不同的数据库上运行(不是默认设置)。 运行测试时,出现以下错误:

AssertionError: Database queries to 'cache' are not allowed in this test. Add 'cache' to users.tests.UserLogTestCase.databases to ensure proper test isolation and silence this failure.

将“缓存”数据库添加到TestCase的数据库变量即可解决该问题(或将其设置为“ __all__”),问题是必须在每个测试中完成。

还有其他(更全球化的)方式来解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我刚遇到这个问题。在很早以前就放弃的环境中,我从一个通用类MyTestCase继承了所有测试用例。我在功能中添加了mixins:

class MyTestCase(django.test.TestCase, ViewTestMixin, AssertResponseStatusMixin, CustomAssertions):

    pass


class MyAPITestCase(rest_framework.test.APITestCase, ViewTestMixin, AssertResponseStatusMixin, CustomAssertions, RedisClearMixin):

    pass