gpt4 book ai didi

python - 用于测试包的基本毒理学设置

转载 作者:行者123 更新时间:2023-12-01 06:49:27 27 4
gpt4 key购买 nike

我正在尝试使用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 安装modules,即setup.py 期望找到mypackage.py。没有找到它,它不会将任何内容打包到 sdist 中。

您需要将mypackage安装为程序包。将行替换为

packages=["mypackage"],

甚至与

packages=find_packages(),

导入find_packages:

from setuptools import setup, find_packages

关于python - 用于测试包的基本毒理学设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59087025/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com