测试包的基本 tox 设置

Basic tox setup to test a package

我正在尝试使用 tox 来测试我正在编写的包,但我无法让它工作。我的 setup.cfg 包含以下部分:

[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

我有一个 setup.py 文件来安装我的模块,如果我 运行 pip install -e .,一切正常,我可以正常使用该模块。

现在,如果我 运行:

tox -e py36,py37,py36-black -p all

py36-black 的测试成功,但 pytest 失败,因为找不到 mypackage。基本上,tox 似乎没有在它创建的虚拟环境中安装我自己的包。

我是否必须在 [testenv] 下的 commands 中手动 运行 pip?还是我在 setup.cfg 中做错了什么?


这是我的 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",
)

我的目录结构是:

mypackage/
    __init__.py
    ...
tests/
    test_mypackage.py
setup.py
setup.cfg

来自 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)

您的 setup.py 中有错误:

py_modules=["mypackage"],

py_modules 安装 模块 ,即 setup.py 期望找到 mypackage.py。找不到它不会将任何东西打包到 sdist 中。

您需要将 mypackage 作为 软件包 安装。将行替换为

packages=["mypackage"],

甚至

packages=find_packages(),

正在导入 find_packages:

from setuptools import setup, find_packages