直接症状:
$ pytest …/apps/users/test/test_graph_permissions_command.py
============================= test session starts ==============================
platform linux -- Python 3.6.8, pytest-4.6.2, py-1.8.0, pluggy-0.12.0
Django settings: dms.settings (from ini file)
rootdir: /src/app, inifile: pytest.ini
plugins: forked-1.0.2, celery-4.3.0, django-3.5.0, cov-2.7.1, xdist-1.28.0
collected 1 item
…/apps/users/test/test_graph_permissions_command.py E
==================================== ERRORS ====================================
_ ERROR at setup of TestGraphPermissionsCommand.test_should_resolve_a_permission_hierarchy _
.venv/lib/python3.6/site-packages/pytest_django/plugin.py:519: in _django_setup_unittest
request.getfixturevalue("django_db_setup")
.venv/lib/python3.6/site-packages/pytest_django/fixtures.py:108: in django_db_setup
**setup_databases_args
.venv/lib/python3.6/site-packages/django/test/utils.py:174: in setup_databases
serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
.venv/lib/python3.6/site-packages/django/db/backends/base/creation.py:80: in create_test_db
self.connection._test_serialized_contents = self.serialize_db_to_string()
.venv/lib/python3.6/site-packages/django/db/backends/base/creation.py:123: in serialize_db_to_string
serializers.serialize("json", get_objects(), indent=None, stream=out)
.venv/lib/python3.6/site-packages/django/core/serializers/__init__.py:128: in serialize
s.serialize(queryset, **options)
.venv/lib/python3.6/site-packages/django/core/serializers/base.py:90: in serialize
for count, obj in enumerate(queryset, start=1):
.venv/lib/python3.6/site-packages/django/db/backends/base/creation.py:120: in get_objects
yield from queryset.iterator()
.venv/lib/python3.6/site-packages/django/db/models/query.py:341: in _iterator
yield from self._iterable_class(self, chunked_fetch=use_chunked_fetch, chunk_size=chunk_size)
.venv/lib/python3.6/site-packages/polymorphic/query.py:56: in _polymorphic_iterator
o = next(base_iter)
.venv/lib/python3.6/site-packages/django/db/models/query.py:55: in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
.venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py:1087: in execute_sql
sql, params = self.as_sql()
.venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py:474: in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
.venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py:54: in pre_sql_setup
self.setup_query()
.venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py:45: in setup_query
self.select, self.klass_info, self.annotation_col_map = self.get_select()
.venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py:219: in get_select
cols = self.get_default_columns()
.venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py:666: in get_default_columns
column = field.get_col(alias)
.venv/lib/python3.6/site-packages/django/db/models/fields/related.py:981: in get_col
output_field = self.target_field
.venv/lib/python3.6/site-packages/django/db/models/fields/related.py:878: in target_field
return self.foreign_related_fields[0]
.venv/lib/python3.6/site-packages/django/db/models/fields/related.py:632: in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
.venv/lib/python3.6/site-packages/django/db/models/fields/related.py:619: in related_fields
self._related_fields = self.resolve_related_fields()
.venv/lib/python3.6/site-packages/django/db/models/fields/related.py:604: in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
E ValueError: Related model 'appname.ModelName' cannot be resolved
=========================== 1 error in 5.54 seconds ============================
测试文件:
$ cat …/apps/users/test/test_graph_permissions_command.py
from django.test import TestCase
class TestGraphPermissionsCommand(TestCase):
def test_should_resolve_a_permission_hierarchy(self) -> None:
pass
情况:
TestCase
导入unittest
可以,但是在实际测试中我需要django.test.TestCase
中的一些东西。./manage.py makemigrations
报告“未检测到更改”。TestCase
类继承而来。ForeignKey
至"appname.ModelName"
的同上。代码覆盖率为98%。这是怎么回事?看来这可能是由于测试数据库构建器未能按正确的顺序执行操作引起的。