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...中点击减号删除)
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Zhou_ZiZi/article/details/130591116
今日推荐