Duplicate key error running tests with django-nose but not with Django's DiscoverRunner

369 Views Asked by At

When I run my tests with the default Django testrunner (django.test.runner.DiscoverRunner), everything works fine. When I run them using nose (for the XML output), I get IntegrityErrors from tests that aren't mine.

Override this method to return an instance of your custom user model ... ERROR

I have no idea what this is or why it's only being run by the django-nose test runner.

Here's the full stack trace for the error

======================================================================
ERROR: Override this method to return an instance of your custom user model
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "auth_user_username_key"
DETAIL:  Key (username)=([email protected]) already exists.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/wagtail/tests/utils/wagtail_tests.py", line 24, in create_test_user
    return user_model.objects.create_superuser(**user_data)
  File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/models.py", line 162, in create_superuser
    return self._create_user(username, email, password, **extra_fields)
  File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/models.py", line 145, in _create_user
    user.save(using=self._db)
  File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 66, in save
    super().save(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 741, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 779, in save_base
    force_update, using, update_fields,
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 870, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 908, in _do_insert
    using=using, raw=raw)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1186, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1335, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_user_username_key"
DETAIL:  Key (username)=([email protected]) already exists.

Nowhere in my tests am I creating a test user with the email address "[email protected]" and I don't see any of my code anywhere in this stack trace, so I have no idea where it's coming from or why it only shows up when I run my tests with django-nose.

1

There are 1 best solutions below

2
darth_mall On

I think what's going on is that django-nose is very aggressive in how it finds tests. One of my test suites was using WagtailPageTests which has a static method create_test_user. I think Nose was finding that method and running it as a test because it contained the word "test".

I ended up switching to unittest-xml-reporting to get the XUnit XML report, since that's the only reason I was using django-nose anyway, and now my tests run just fine.