ModuleNotFoundError:运行pytest时没有名为“dolphin”的模块

2024-06-16 15:45:12 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我使用pytest运行Python 3单元测试,以下是我的代码:

from dolphin.biz.notifier.dingtalk.dingtalk_impl import DingtalkImpl


class TestDingtalk:

    def test_send_message(self):
        dingtalkImpl = DingtalkImpl()
        dingtalkImpl.send_dingtalk_notify_message(1)

然后我运行这个命令来执行路径/home/dolphin/Documents/GitHub/pydolphin/test/cert/dingtalk中的单元测试:

pytest test_dingtalk.py

但显示此错误(仅在单元测试中发生):

ImportError while importing test module '/home/dolphin/Documents/GitHub/pydolphin/test/cert/dingtalk/test_dingtalk.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/home/dolphin/anaconda3/envs/pydolphin/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_dingtalk.py:1: in <module>
    from dolphin.biz.notifier.dingtalk.dingtalk_impl import DingtalkImpl
E   ModuleNotFoundError: No module named 'dolphin'
=========================================================================================== short test summary info ============================================================================================
ERROR test_dingtalk.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================================================================================== 1 error in 0.05s ===============================================================================================

我的项目结构如下:

enter image description here

我尝试过像这样指定python路径:

set PYTHONPATH=/home/dolphin/Documents/GitHub/pydolphin && pytest test_dingtalk.py

仍然不起作用,我该怎么做才能使它起作用?我也试过这样做:

 pytest ./test/cert/dingtalk/test_dingtalk.py

Tags: inpytestimportgithubhomecertpytest