5

I'm running a series of unit tests on a view, but I'm noticing that the database which is being accessed in each test isn't changing.

For example, the first test I might run might have the command:

Post.objects.create(title='TEST1',...)

and if I get the response, the queryset will contain:

However, all tests I run after this will return the same

<Post: TEST1>

No test, other than the first one, creates an object with the title 'TEST1'; they are all created with other numbers, e.g. 'TEST2', 'TEST3', etc.

Additionally, if I run the tests individually, I have no problems, but if I run all the tests for the application, these errors arise. Does anyone know what's happening? I'm using django.test.TestCase for all tests.

thavan
  • 2,409
  • 24
  • 32
fizzy123
  • 81
  • 6
  • 1
    What happens if you inherit your test case class from [`django.test.TransactionTestCase`](https://docs.djangoproject.com/en/dev/topics/testing/overview/#django.test.TransactionTestCase) (instead of `django.test.TestCase`) for these tests? – Anton Strogonoff Apr 21 '13 at 04:50
  • Nothing changes. I've also tried unittest.TestCase. – fizzy123 Apr 21 '13 at 04:52
  • 1
    Odd, `TransactionTestCase` at least should truncate the table after each test… But this sounds familiar. Which DB engine do you use for tests? SQLite or, say, PostgreSQL? – Anton Strogonoff Apr 21 '13 at 04:54
  • You can try switching DB backend to SQLite in your test settings and see if it helps. Otherwise I fail to help here, either it's a bug in Django, or something with your tests. Seems related to http://stackoverflow.com/questions/15124828/django-test-database-not-auto-flushing (unanswered, solved questions usually involve `TransactionTestCase`). – Anton Strogonoff Apr 21 '13 at 05:16
  • I have the same scenario with SQLite – Mikhail Nov 21 '13 at 03:39

0 Answers0