3

I'm getting the exact same issue as when does the database is being destroy in django tests? , where my test DB seems to be getting deleted between each method. I know it's being cleared out each time I re-run python3 manage.py test , but it shouldn't be deleted in the middle of the test.

I'm running Python 3.4.3, Postgresql 9.5.3, Django 1.9

from django.test import TestCase
class myTestCases(TestCase):
    def test_1_load_regions(self):
        MyMethods._updateRegions()
        self.assertEqual(True, len(Region.objects.all()) >= minRegionsExpected)
        print("Regions: %s Languages: %s"%(len(Region.objects.all()), len(Language.objects.all())))

    def test_2_load_languages(self): 
        # Generated by _updateRegions, just check that a few languages exist
        print("Regions: %s Languages: %s"%(len(Region.objects.all()), len(Language.objects.all())))

        self.assertEqual(True, len(Language.objects.all()) >= minLanguagesExpected)

And I'm get an output like this:

Regions: 11 Languages: 19
.Regions: 0 Languages: 0
F

That makes me think everything is saving when the first test ends, but somehow when the second test starts everything is cleared out. I would rather avoid re-running everything at the start of each test, but right now I'm stumped at how to get the test DB to actually keep my results...

Edit/Results: So after some poking in the right direction from the comments and answers, I found what I was looking for. https://docs.djangoproject.com/en/1.9/topics/testing/overview/

Warning

If your tests rely on database access such as creating or querying models, be sure to create your test classes as subclasses of django.test.TestCase rather than unittest.TestCase.

Using unittest.TestCase avoids the cost of running each test in a transaction and flushing the database, but if your tests interact with the database their behavior will vary based on the order that the test runner executes them. This can lead to unit tests that pass when run in isolation but fail when run in a suite.

I was using test_1, test_2, test_3 as my names to ensure the order of operations, so that wasn't a problem. By swapping from django.test.TestCase to unittest.TestCase I got the results I wanted, with my database persisting between each test case.

5
  • 2
    Er, yes it should be. That is how tests are supposed to work. Commented Jul 14, 2016 at 15:50
  • Yes, when you run tests, a new, empty database is created. It does not carry data over from your "primary" db. Commented Jul 14, 2016 at 15:55
  • @Justin I know it's different from my main DB, but it should stick around in the Test DB between test_1 and test_2, right? Otherwise what's the point of even having a DB, much less the command line option to not delete the DB after the tests? Commented Jul 14, 2016 at 16:00
  • Oh, my apologies, I misunderstood your question. Commented Jul 14, 2016 at 16:05
  • @DanielRoseman No, that's not how tests are suppose to work. Otherwise the --keepdb option wouldn't exist, as it would always be saving an empty database, which has no benefit over creating a brand new database. Commented Jul 14, 2016 at 16:28

1 Answer 1

3

Actually, according to the Django tutorial, the database is rolled back between each test. (See the bottom of the linked section.)

If you're looking to have a common setup between tests, you should consider overriding the TestCase method setUp. This is run before each test function. The unittest documentation should be helpful for this, and Django has an example in their documentation as well.

Sign up to request clarification or add additional context in comments.

2 Comments

Any idea why they would have an arg --keepdb then? Seems pretty useless if the DB is destroyed between each test, you'd be left with a mostly empty DB.
@Dr.Cyanide Even before the tests start, the database isn't totally empty, though. All of your migrations have to be applied before running the first test to set up the tables in the database. So it looks like the benefit of --keepdb is that it means you don't have to re-run migrations each time. But I'm definitely not an expert on this, so maybe someone else knows more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.