Pycharm运行unittest报错ModuleNotFoundError: No module named ‘pytest‘解决

使用unittest未import pytest相关功能语句,在pycharm中右键run的时候报错:

Traceback (most recent call last):
  File "B:\Application\pycharm\PyCharm 2023.1\plugins\python\helpers\pycharm\_jb_pytest_runner.py", line 5, in <module>
    import pytest
ModuleNotFoundError: No module named 'pytest'

Process finished with exit code 1

Empty suite

原因:

直接在 PyCharm 中点击运行按钮,PyCharm 会自动配置并运行 pytest。因为 PyCharm 内部默认使用 pytest 进行单元测试。

解决方法1(单个文件)

  1. 在 PyCharm 的菜单栏中,依次选择 Run -> Edit Configurations...
  2. 在打开的窗口中选择 + -> Python tests -> Unittests


3. 在 Target 中选择需要运行的 unittest 文件,点击 OK 即可保存配置;

  1. 之后,就可以在菜单栏中选择 Run -> Run 'Unittests' 来运行 unittest 了

解决方法2(所有文件)

  1. 打开 PyCharm,在菜单栏中选择 File -> Settings;
  2. 在弹出的 Settings 窗口中,选择 Tools -> Python Integrated Tools,在右侧的 Default test runner 下拉框中选择 unittest,点击 Apply 和 OK 按钮保存更改。
  3. 之后,就可以在菜单栏中选择 Run -> Run 'Unittests' 来运行 unittest 了(之前已经生成的pytest需要在Run -> Edit Configurations...中点击减号删除)

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年9月2日
下一篇 2023年9月2日

相关推荐