1

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)=(test@email.com) 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)=(test@email.com) already exists.

Nowhere in my tests am I creating a test user with the email address "test@email.com" 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.

darth_mall
  • 984
  • 8
  • 16

1 Answers1

1

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.

darth_mall
  • 984
  • 8
  • 16
  • If you're inheriting from `WagtailPageTests` in your tests, then it will [call `self.login` on setup](https://github.com/wagtail/wagtail/blob/6358d84fa9534277828c17b6a381bc46abb119a0/wagtail/tests/utils/page_tests.py#L14), which in turn [calls `create_test_user`](https://github.com/wagtail/wagtail/blob/6358d84fa9534277828c17b6a381bc46abb119a0/wagtail/tests/utils/wagtail_tests.py#L28), so that's happening by design. – gasman Aug 06 '19 at 16:27
  • Right, but there's something about the `django-nose` runner that (apparently) causes `create_test_user` to be called multiple times resulting in the `ValidationError` and `django-nose` is interpreting it as a test that is erroring out. I suspect it isn't by design that suites inheriting from `WagtailPageTests` always error out. And since this doesn't happen with either `django.test.runner.DiscoverRunner` or `xmlrunner.extra.djangotestrunner.XMLTestRunner`, I suspect there's an issue combining `django-nose` (with its default configuration) with `WagtailPageTests`. – darth_mall Aug 06 '19 at 20:11