Using unittest without importing pytest-related function statements, an error is reported when right-clicking run in pycharm:
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)
- In the menu bar of PyCharm, select
Run
-> in turnEdit Configurations...
;
- In the opened window select
+
->Python tests
->Unittests
;
3. Target
Select the unittest file to be run in , and click OK
to save the configuration;
- After that, you can select
Run
-> in the menu barRun 'Unittests'
to run unittest
Workaround 2 (all files)
- Open PyCharm, select File -> Settings in the menu bar;
- 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.
Run
After that, you can select -> in the menu barRun 'Unittests'
to run unittest (the pytest that has been generated before needs to be deleted by clicking the minus sign inRun
->Edit Configurations...
)