Pycharm runs unittest and reports ModuleNotFoundError: No module named 'pytest' to solve

Using unittest without importing pytest-related function statements, an error is reported when right-clicking run in pycharm:
insert image description here

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

reason:

Click the run button directly in PyCharm, PyCharm will automatically configure and run pytest. Because PyCharm internally uses pytest for unit testing by default.

Workaround 1 (single file)

  1. In the menu bar of PyCharm, select Run-> in turn Edit Configurations...;
    insert image description here
  2. In the opened window select +-> Python tests-> Unittests;

insert image description here
3. TargetSelect the unittest file to be run in , and click OKto save the configuration;

insert image description here

  1. After that, you can select Run-> in the menu bar Run 'Unittests'to run unittest
    insert image description here

Workaround 2 (all files)

  1. Open PyCharm, select File -> Settings in the menu bar;
    insert image description here
  2. In the pop-up Settings window, select Tools -> Python Integrated Tools, select unittest in the Default test runner drop-down box on the right, and click the Apply and OK buttons to save the changes.
    insert image description here
  3. RunAfter that, you can select -> in the menu bar Run 'Unittests'to run unittest (the pytest that has been generated before needs to be deleted by clicking the minus sign in Run-> Edit Configurations...)
    insert image description here

Guess you like

Origin blog.csdn.net/Zhou_ZiZi/article/details/130591116