How do I get Pytest to look in my project? #9693
-
By default, pytest looks in the Here's my directory tree and the output of
dillonbarnes at Downstairs-iMac in ~/Documents/Coding/GitStats on master!
± pytest -v
======================================================================================== test session starts =========================================================================================
platform darwin -- Python 3.10.0, pytest-7.0.1, pluggy-1.0.0 -- /Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10
cachedir: .pytest_cache
rootdir: /Users/dillonbarnes/Documents/Coding/GitStats
collected 0 items / 1 error
=============================================================================================== ERRORS ===============================================================================================
_______________________________________________________________________________ ERROR collecting tests/test_github.py ________________________________________________________________________________
ImportError while importing test module '/Users/dillonbarnes/Documents/Coding/GitStats/tests/test_github.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/test_github.py:2: in <module>
from pygitapi import HubAPI
E ModuleNotFoundError: No module named 'pygitapi'
====================================================================================== short test summary info =======================================================================================
ERROR tests/test_github.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================================== 1 error in 0.11s ========================================================================================== Is this a bug or just something I'm doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi @DillonB07, See https://docs.pytest.org/en/stable/reference/reference.html#confval-norecursedirs, specially:
So not sure why this detection is not working, but regardless you can configure Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
(Disregard what I wrote earlier; I misunderstood your problem.) So your problem is that your tests try to import your project source, but it's not found. This isn't a pytest thing; it's a basic Python packaging/importing thing. In order for your package to be importable, it needs to be in However! This is not good packaging/testing practice. What you should do instead is install your package into a virtualenv that also contains pytest, and run pytest inside that venv. This can be done with tox or its cousin nox. Moreover, you should move your package code (not your tests!) inside a |
Beta Was this translation helpful? Give feedback.
(Disregard what I wrote earlier; I misunderstood your problem.)
So your problem is that your tests try to import your project source, but it's not found. This isn't a pytest thing; it's a basic Python packaging/importing thing. In order for your package to be importable, it needs to be in
sys.path
. When you run justpytest
, the current directory will not be insys.path
, but it will be if you instead runpython -m pytest
. (At least, I'm 99% sure that's how it works; I can't find where this is mentioned in the pytest documentation at the moment.) So the simple solution is to runpython -m pytest
instead of justpytest
.However! This is not good packaging/testing practice. What you should do…