我一直在尝试为 pytest 制作一些单元测试。当我执行“python manage.py runserver”时,服务器运行得很好。但是,当我执行 'pytest' 时,它显示以下错误:
============================ test session starts =============================
platform darwin -- Python 3.7.9, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
django: settings: tradeforce.settings.dev (from ini)
rootdir: /Users/woohyunan/projects/work/tradir/backend-app, configfile: pytest.ini
plugins: fixture-order-0.1.3, drf-1.1.2, common-subject-1.0.5, lambda-1.2.4, django-4.1.0, assert-utils-0.2.1
collected 0 items
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/python.py", line 578, in _importtestmodule
INTERNALERROR> mod = import_path(self.fspath, mode=importmode)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/pathlib.py", line 531, in import_path
INTERNALERROR> importlib.import_module(module_name)
INTERNALERROR> File "/Users/woohyunan/.pyenv/versions/3.7.9/lib/python3.7/importlib/__init__.py", line 127, in import_module
INTERNALERROR> return _bootstrap._gcd_import(name[level:], package, level)
INTERNALERROR> File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
INTERNALERROR> File "<frozen importlib._bootstrap>", line 983, in _find_and_load
INTERNALERROR> File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
INTERNALERROR> File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/assertion/rewrite.py", line 170, in exec_module
INTERNALERROR> exec(co, module.__dict__)
INTERNALERROR> File "/Users/woohyunan/projects/work/tradir/backend-app/contacts/tests/integration_tests.py", line 4, in <module>
INTERNALERROR> from contacts.api.views import CustomerCreateAPIView
INTERNALERROR> File "/Users/woohyunan/projects/work/tradir/backend-app/contacts/api/views.py", line 15, in <module>
INTERNALERROR> from contacts.api.serializers import (
INTERNALERROR> File "/Users/woohyunan/projects/work/tradir/backend-app/contacts/api/serializers.py", line 7, in <module>
INTERNALERROR> import user.api.serializers
INTERNALERROR> File "/Users/woohyunan/projects/work/tradir/backend-app/user/api/serializers.py", line 20, in <module>
INTERNALERROR> from contacts.api.serializers import (
INTERNALERROR> ImportError: cannot import name 'CustomerIdNameSerializer' from 'contacts.api.serializers' (/Users/woohyunan/projects/work/tradir/backend-app/contacts/api/serializers.py)
INTERNALERROR>
INTERNALERROR> The above exception was the direct cause of the following exception:
INTERNALERROR>
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/main.py", line 269, in wrap_session
INTERNALERROR> session.exitstatus = doit(config, session) or 0
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/main.py", line 322, in _main
INTERNALERROR> config.hook.pytest_collection(session=session)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in <lambda>
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/main.py", line 333, in pytest_collection
INTERNALERROR> session.perform_collect()
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/main.py", line 634, in perform_collect
INTERNALERROR> self.items.extend(self.genitems(node))
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/main.py", line 808, in genitems
INTERNALERROR> rep = collect_one_node(node)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/runner.py", line 457, in collect_one_node
INTERNALERROR> ihook.pytest_collectstart(collector=collector)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in <lambda>
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/pytest_lambda/plugin.py", line 11, in pytest_collectstart
INTERNALERROR> process_lambda_fixtures(collector.module)
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/python.py", line 272, in module
INTERNALERROR> return node.obj if node is not None else None
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/python.py", line 291, in obj
INTERNALERROR> self._obj = obj = self._getobj()
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/python.py", line 500, in _getobj
INTERNALERROR> return self._importtestmodule()
INTERNALERROR> File "/Users/woohyunan/myvenv/lib/python3.7/site-packages/_pytest/python.py", line 608, in _importtestmodule
INTERNALERROR> ) from e
INTERNALERROR> _pytest.nodes.Collector.CollectError: ImportError while importing test module '/Users/woohyunan/projects/work/tradir/backend-app/contacts/tests/integration_tests.py'.
INTERNALERROR> Hint: make sure your test modules/packages have valid Python names.
INTERNALERROR> Traceback:
INTERNALERROR> ../../../../.pyenv/versions/3.7.9/lib/python3.7/importlib/__init__.py:127: in import_module
INTERNALERROR> return _bootstrap._gcd_import(name[level:], package, level)
INTERNALERROR> contacts/tests/integration_tests.py:4: in <module>
INTERNALERROR> from contacts.api.views import CustomerCreateAPIView
INTERNALERROR> contacts/api/views.py:15: in <module>
INTERNALERROR> from contacts.api.serializers import (
INTERNALERROR> contacts/api/serializers.py:7: in <module>
INTERNALERROR> import user.api.serializers
INTERNALERROR> user/api/serializers.py:20: in <module>
INTERNALERROR> from contacts.api.serializers import (
INTERNALERROR> E ImportError: cannot import name 'CustomerIdNameSerializer' from 'contacts.api.serializers' (/Users/woohyunan/projects/work/tradir/backend-app/contacts/api/serializers.py)
这似乎不是循环导入的情况,因此我无法确定此错误的任何原因。以下是我的“联系人”应用程序的结构
./contacts
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── admin.cpython-37.pyc
│ ├── models.cpython-37.pyc
│ └── tests.cpython-37-pytest-6.2.2.pyc
├── admin.py
├── api
│ ├── __pycache__
│ │ ├── permissions.cpython-37.pyc
│ │ ├── serializers.cpython-37.pyc
│ │ ├── urls.cpython-37.pyc
│ │ └── views.cpython-37.pyc
│ ├── permissions.py
│ ├── serializers.py
│ ├── urls.py
│ └── views.py
├── apps.py
├── management
│ └── commands
│ └── upload_country.py
├── migrations
│ ├── 0001_initial.py
│ ├── 0002_auto_20200908_1917.py
│ ├── 0003_auto_20201105_0429.py
│ ├── 0004_accountattachment_uploader.py
│ ├── 0005_auto_20201106_0606.py
│ ├── 0006_auto_20201110_0638.py
│ ├── 0007_country_code.py
│ ├── 0008_accountattachment_key.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-37.pyc
│ ├── 0002_auto_20200908_1917.cpython-37.pyc
│ ├── 0003_auto_20201105_0429.cpython-37.pyc
│ ├── 0004_accountattachment_uploader.cpython-37.pyc
│ ├── 0005_auto_20201106_0606.cpython-37.pyc
│ ├── 0006_auto_20201110_0638.cpython-37.pyc
│ ├── 0007_country_code.cpython-37.pyc
│ ├── 0008_accountattachment_key.cpython-37.pyc
│ └── __init__.cpython-37.pyc
├── models.py
└── tests
├── __pycache__
│ └── integration_tests.cpython-37-pytest-6.2.2.pyc
├── factories.py
├── integration_tests.py
└── unit_tests.py
有问题的序列化器位于: 联系人/api/serializers.py
.
.
.
class CustomerIdNameSerializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = ('id', 'name', )
.
.
.
如果不是循环导入造成的,在做测试的时候怎么会出现这个错误?
答案 0 :(得分:1)
from app_name import api
# to use serializer
serializer=api.serializeFileName