[Fixed]-Unit tests fail after a Django upgrade

8πŸ‘

βœ…

I have uncovered multiple issues with my unit test coverage, but to answer the question I posted, two separate issues were causing the errors I described.

Django 1.11 is stricter about its model instance creation and we had some legacy test code that was not updated to new model structure. For example, if you take the default User model, in Django 1.8 you can do

from django.contrib.auth.models import User    
User.objects.create(username="something", password="something", something="something")

But in Django 1.11 it will raise an exception "TypeError: 'something' is an invalid keyword argument for this function".

And the second issue is that Django TestCase wraps test cases in two separate atomic blocks. This helps with keeping class level setup separate from instance level setup, but it also introduces a subtle problem. If a test crashes with a database error you never see the error because it happens in the test level atomic and not in the class level atomic. The test level atomic fails and database connection gets dropped. After that every test is going to fail with the exact django.db.utils.InterfaceError: connection already closed error I was seeing. Switching from TestCase to TransactionTestCase caused a lot of these problems to be exposed directly in the test output.

πŸ‘€Mad Wombat

3πŸ‘

It may be useful to have the error trace.
But for the moment in the code I see this error:

   self.user = User.objects.create_superuser(
                self.username,
                self.username, **this should be self.email**
                self.password
            )
πŸ‘€wololoooo

Leave a comment