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

Related Articles
Тестирование
Python

Search on this site

Subscribe to @aofeed channel for updates

Visit Channel

@aofeed

Feedbak and Questions in Telegram

@aofeedchat