1

当我使用默认的 Django testrunner ( django.test.runner.DiscoverRunner) 运行测试时,一切正常。当我使用nose(用于XML 输出)运行它们时,我IntegrityErrors从不属于我的测试中得到。

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

我不知道这是什么或为什么它只由 django-nose 测试运行程序运行。

这是错误的完整堆栈跟踪

======================================================================
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.

在我的测试中,我没有创建一个电子邮件地址为“test@email.com”的测试用户,而且我在这个堆栈跟踪中的任何地方都看不到我的任何代码,所以我不知道它来自哪里或为什么它只当我用 django-nose 运行测试时出现。

4

1 回答 1

1

我认为发生的事情是 django-nose 在查找测试方面非常激进。我的一个测试套件正在使用WagtailPageTests它有一个静态方法create_test_user。我认为 Nose 正在找到该方法并将其作为测试运行,因为它包含“测试”一词。

我最终切换到unittest-xml-reporting获取 XUnit XML 报告,因为这是我使用 django-nose 的唯一原因,现在我的测试运行得很好。

于 2019-08-06T16:22:48.040 回答