Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django unit tests failing when run with other test cases

I'm getting inconsistent behavior with Django unit tests. On my development machine using sqlite, if I run tests on my two apps separately the tests pass, but if I run manage.py test to test everything at once, I start getting unit test failures consistently on two tests.

On my staging server which uses Postgres, I have a particular test that works when testing it individually (e.g. manage.py test MyApp.tests.MyTestCase.testSomething), but fails when running the entire test case (e.g. manage.py test MyApp.tests.TestCase).

Other related StackOverflow questions seem to have two solutions:

  1. Use Django TestCase's instead of the Python equivalent
  2. Use TransactionTestCase's to make sure the database is cleaned up properly after every test.

I've tried both to no avail. Out of frustration, I also tried using django-nose instead, but I was seeing the same errors. I'm on Django 1.6.

like image 983
mathew Avatar asked Dec 09 '13 22:12

mathew


1 Answers

Besides using TestCase for all your tests, you need to make sure you tear down any patching that was done in your setup methods:

def setUp(self):
    self.patcher = patch('my.app.module')

def tearDown(self):
    self.patcher.stop()
like image 100
rgilligan Avatar answered Nov 15 '22 05:11

rgilligan