PyTest Errors

Contents
Intro
Example
Skeleton
Add PyTest to PyCharm
Related Articles

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 ===========================================================

This error can occur if the test is in a separate directory, for example tests, and is running from there, but the import of the module under test is done in the simplest way - just like from id 1 import fun 1

PyTest cannot find the module that needs to be tested. Most likely, running the test from the root directory.

That is, instead of

python -m pytest test_something.py

Run

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

NameError: name 'pytest' is not defined

Need to import pytest first.

import pytest

Article author: Andrei Olegovich

Related articles
Тестирование
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.ru
Write to info@urn.su, 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: