I'm playing with this repo https://github.com/mCodingLLC/SlapThatLikeButton-TestingStarterProject. It shows automated testing in Python with pytest and tox.
After cloning the repo the following steps are performed:
- create a virtualenv
pip install -r requirements_dev.txtpip install -e .pytestruns successfullytoxruns successfully
I've made a slight change to the layout of src directory. Added a subpackage src/models, moved slap_that_like_button.py to this subpackage and updated tests accordingly. Now the tree and the test looks like this:
src
├── slapping
├── __init__.py
├── models
│ ├── __init__.py
│ └── slap_that_like_button.py
└── py.typed
diff --git a/tests/test_slapping.py b/tests/test_slapping.py
index 40ed2f4..8298ec5 100644
--- a/tests/test_slapping.py
+++ b/tests/test_slapping.py
@@ -1,5 +1,5 @@
import pytest
-from slapping.slap_that_like_button import LikeState, slap_many
+from slapping.models.slap_that_like_button import LikeState, slap_many
def test_empty_slap():
Results:
pytestruns successfullytoxfails when runspytestwithModuleNotFoundError: No module named 'slapping.models'. Full traceback https://gist.github.com/ttl256/1d77c1b62801e7b78fec6b116d307eb4
- Why is it that
pytestcan successfully run from my terminal but can't find the subpackage when launched withintox? - How moving a module to a subpackage makes so much difference?
P.S. For anyone who wants play with the repo. Bump tox version in requirements_dev.txt to 4.11.4. Otherwise it gives this traceback https://gist.github.com/ttl256/76499d8a3b1db0250d571a7fdd7d3b06
EDIT. Installing the package as editable solves the issue however root cause is still unknown.
$ git diff tox.ini
diff --git a/tox.ini b/tox.ini
index 6b58ff8..8730352 100644
--- a/tox.ini
+++ b/tox.ini
@@ -13,6 +13,7 @@ python =
[testenv]
setenv =
PYTHONPATH = {toxinidir}
+package = editable
deps =
-r{toxinidir}/requirements_dev.txt
commands =
There is
packagesdefinition in the option section ofsetup.cfg:When a new package was added
setuptoolsdidn't know about it. There are a couple of ways to signalsetuptoolsabout a new package andfindworks nicely here as it saves us from manually adding each package for this simple project.Now as we add more packages
setuptoolswill automatically discover them.Reference on custom package discovery: https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#custom-discovery