I have a python project structure using poetry as package manager and pytest to run tests. The project has a structure like:
.github/
workflows/
test_workflow.yml
src/
__init__.py
exceptions.py
tests/
test_utils.py
pyproject.toml
poetry.lock
Now, inside test_utils.py
there are tests that import some exceptions from exceptions.py
:
from src.exceptions import DateNotFoundError, ParameterNotFoundError
This is all fine, running poetry run pytest
inside a venv
in my local environment works correctly and the test are passing.
Now I'm trying to set up a GitHub action to run the tests on each commit. What I have is this:
name: test
run-name: Run unit tests
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11.4'
- name: Install dependencies
run: |
python -m pip install poetry==1.6.1
poetry install
- name: Run tests
run: poetry run pytest
Which throws the following error in github actions: ModuleNotFoundError: No module named 'src'
This is the complete stacktrace:
==================================== ERRORS ====================================
_________________ ERROR collecting tests/test_utils.py __________________
ImportError while importing test module '/home/runner/work/MyProject/MyProject/tests/test_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../../.cache/pypoetry/virtualenvs/myproject-n2wF6nQj-py3.11/lib/python3.11/site-packages/_pytest/python.py:617: in _importtestmodule
mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
../../../.cache/pypoetry/virtualenvs/myproject-n2wF6nQj-py3.11/lib/python3.11/site-packages/_pytest/pathlib.py:567: in import_path
importlib.import_module(module_name)
/opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1204: in _gcd_import
???
<frozen importlib._bootstrap>:1176: in _find_and_load
???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:690: in _load_unlocked
???
../../../.cache/pypoetry/virtualenvs/myproject-n2wF6nQj-py3.11/lib/python3.11/site-packages/_pytest/assertion/rewrite.py:186: in exec_module
exec(co, module.__dict__)
tests/test_utils.py:3: in <module>
from src.exceptions import DateNotFoundError, ParameterNotFoundError
E ModuleNotFoundError: No module named 'src'
=========================== short test summary info ============================
ERROR tests/test_utils.py
=============================== 1 error in 1.42s ===============================
make: *** [Makefile:18: test] Error 1
Error: Process completed with exit code 2.
I already tried different Github actions to install poetry, enabling virtual environments through the yaml file and adding export PYTHONPATH="$PYTHONPATH:$PWD"
to the workflow but all yield the exact same result. Thanks!
This issue was fixed by adding a __init__.py
file to the tests/
directory.