I created a module named stuff, and in stuff folder, I created these files, visual studio code preview
__init__.py
, and accum.py
in accum.py
I have
class Accumulator:
def __init__(self):
self._count = 0
@property
def count(self):
return self._count
def add(self, more=1):
self._count += more
I created a new folder test
, and in the test
folder I created a file test_accum.py
in test_accum.py
file I have
import pytest
from stuff.accum import Accumulator
when I run the Python file it returns:
ModuleNotFoundError: No module named 'stuff'
If you try to run the test module, it fails because it is not in the path like @tromgy explained. However, to run tests you don't need the init files or mess with sys.path
.
With pytest, you are not supposed to run the test module itself to run tests, but instead run pytest from command line with python -m pytest tests
. Just make sure you are running from your project folder i.e. "tau-intro-to-pytest". If you want to run a specific test function, you can specify that from the command line wtih python -m pytest tests/test_mod.py::test_func
, but there are good VS Code extensions to do that without writing lengthy command line calls. The python extension has a tests explorer included in it, but I like the UI of Python Test Explorer for VS Code better.