Ошибки PyTest

Contents
Введение
Пример применения
Структура проекта
Добавить PyTest в PyCharm
Похожие статьи

ImportError while importing test module

========================================================= test session starts ========================================================== platform linux -- Python 3.9.5, pytest-7.0.1, pluggy-1.0.0 rootdir: /home/andrei/python/pytest/app/tests collected 0 items / 1 error ================================================================ ERRORS ================================================================ ____________________________________________________ ERROR collecting test_psum.py _____________________________________________________ ImportError while importing test module '/home/andrei/python/pytest/app/tests/test_psum.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: ../../../../.pyenv/versions/3.9.5/lib/python3.9/importlib/__init__.py:127: in import_module return _bootstrap._gcd_import(name[level:], package, level) /home/andrei/python/pytest/qa2/tests/test_psum.py:1: in ??? E ModuleNotFoundError: No module named 'psum' ======================================================= short test summary info ======================================================== ERROR test_psum.py !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! =========================================================== 1 error in 0.06s ===========================================================

Такая ошибка может возникнуть если тест лежит в одтельной директории, например tests, и запущен оттуда же, но импорт тестируемого модуля сделан самым простым способом - просто как from mod1 import fun1

PyTest не может найти модуль, который нужно протестировать. Скорее всего поможет запуск теста из корневой директории.

То есть вместо

python -m pytest test_something.py

Выполните

cd ..
python -m pytest tests/test_something.py

Article author: Andrei Olegovich

Related articles
Testing
Python

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: