5

I'm migrating project to django 2.2 from django 1.11. We have database based cache that runs on different database (not on default). When running tests, we get the following error:

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.

Adding the 'cache' database to the databases variable of the TestCase solves the problem (or setting it to '__all__'), the problem is that this has to be done per test.

Is there any other (more global) way to solve this?

eran
  • 6,731
  • 6
  • 35
  • 52

1 Answers1

2

I just hit this problem. In my environment I gave up long ago and inherit all test cases from a common class, MyTestCase. I add the features in with mixins:

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

    pass


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

    pass

I add the databases variable to these base classes to apply globally.

rrauenza
  • 6,285
  • 4
  • 32
  • 57