问题
I am trying to use tox
to test a package I am writing and I cannot get it to work. My setup.cfg
contains the following section:
[tox]
envlist = py36,py37,py36-black
[testenv]
deps =
pytest
commands =
pytest tests
[testenv:py36-black]
deps =
black
flake8
flake8-black
mypy
commands =
black --check --diff setup.py mypackage tests
flake8 mypackage tests
mypy mypackage tests
I have a setup.py
file to install my module, and if I run pip install -e .
, everything works well and I can use the module normally.
Now, if I run:
tox -e py36,py37,py36-black -p all
The tests for py36-black
succeed but pytest
fails because it cannot find mypackage
. Basically, it seems that tox
does not install my own package in the virtual environments it creates.
Do I have to run pip
manually in the commands
under [testenv]
? Or did I do something wrong in my setup.cfg
?
Here is my setup.py
:
from setuptools import setup
with open("README.md", "r") as fp:
long_description = fp.read()
install_requires = ["numpy", "scipy"]
test_requires = ["pytest", "mypy", "black", "flake8", "flake8-black"]
setup(
name="mypackage",
version="0.0.1",
author="Holt",
install_requires=install_requires,
test_requires=test_requires,
extras_require={"test": test_requires},
py_modules=["mypackage"],
long_description=long_description,
license="MIT",
python_requires=">=3.5",
)
My directory structure is:
mypackage/
__init__.py
...
tests/
test_mypackage.py
setup.py
setup.cfg
Full logs from tox --recreate -e py36 -p all
:
action: py36, msg: parallel py36
cwd: /home/holt/Dev/python/mypackage
cmd: /usr/bin/python3.6 /usr/lib/python3.6/site-packages/tox/__main__.py --recreate -e py36 -p all --installpkg .tox/.tmp/package/1/mypackage-0.0.1.zip
py36 recreate: /home/holt/Dev/python/mypackage/.tox/py36
py36 installdeps: pytest
WARNING: Discarding $PYTHONPATH from environment, to override specify PYTHONPATH in 'passenv' in your configuration.
py36 inst: /home/holt/Dev/python/mypackage/.tox/.tmp/package/2/mypackage-0.0.1.zip
WARNING: Discarding $PYTHONPATH from environment, to override specify PYTHONPATH in 'passenv' in your configuration.
py36 installed: You are using pip version 10.0.1, however version 19.3.1 is available.,You should consider upgrading via the 'pip install --upgrade pip' command.,attrs==19.3.0,importlib-metadata==0.23,mypackage==0.0.1,more-itertools==7.2.0,numpy==1.17.4,packaging==19.2,pluggy==0.13.1,py==1.8.0,pyparsing==2.4.5,pytest==5.3.1,scipy==1.3.3,six==1.13.0,wcwidth==0.1.7,zipp==0.6.0
py36 run-test-pre: PYTHONHASHSEED='789851852'
py36 run-test: commands[0] | pytest tests
============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-5.3.1, py-1.8.0, pluggy-0.13.1
cachedir: .tox/py36/.pytest_cache
rootdir: /home/holt/Dev/python/mypackage
collected 0 items / 1 error
==================================== ERRORS ====================================
____________________ ERROR collecting tests/test_mypackage.py _____________________
ImportError while importing test module '/home/holt/Dev/python/mypackage/tests/test_mypackage.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_mypackage.py:5: in <module>
import mypackage
E ModuleNotFoundError: No module named 'mypackage'
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.10s ===============================
ERROR: InvocationError for command /home/holt/Dev/python/mypackage/.tox/py36/bin/pytest tests (exited with code 2)
回答1:
You have a bug in your setup.py
:
py_modules=["mypackage"],
py_modules
installs modules, that is setup.py
expects to find mypackage.py
. Not finding it it doesn't package anything into sdist.
You need to install mypackage
as a package. Replace the line with
packages=["mypackage"],
or even with
packages=find_packages(),
importing find_packages
:
from setuptools import setup, find_packages
来源:https://stackoverflow.com/questions/59087025/basic-tox-setup-to-test-a-package