从 setup.py pip 安装 tox 的测试依赖项 [英] pip install test dependencies for tox from setup.py

查看:37
本文介绍了从 setup.py pip 安装 tox 的测试依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 setuptools 制作了我的项目,我想用 tox 测试它.我在变量中列出了依赖项并添加到 setup() 参数(tests_requireextras_require).我的项目需要安装 tests_require 中列出的所有依赖项进行测试,但 pip install 没有安装它们.

I made my project with setuptools and I want to test it with tox. I listed dependencies in a variable and added to setup() parameter (tests_require and extras_require). My project needs to install all of the dependencies listed in tests_require to test but pip install is not installing them.

我试过了,但没有用:

install_command = pip install {opts} {packages}[tests]

如何在无需管理多个依赖项列表的情况下安装测试依赖项(即在 test_requirements.txttests_require 变量中列出所有依赖项)?

How can I install test dependencies without having to manage multiple dependency lists (i.e. Having all dependencies listed in both test_requirements.txt and the tests_require variable)?

推荐答案

我通过轻微滥用 额外要求.您几乎已经尝试了 extras 语法,只是 tests_require deps 不会以这种方式自动可用.

I've achieved this by committing a slight abuse of extra requirements. You were almost there trying the extras syntax, just that tests_require deps aren't automatically available that way.

使用这样的 setup.py:

from setuptools import setup

test_deps = [
    'coverage',
    'pytest',
]
extras = {
    'test': test_deps,
}

setup(
    # Other metadata...
    tests_require=test_deps,
    extras_require=extras,
)

然后您可以使用 extras 语法安装测试依赖项,例如从项目根目录:

You can then get the test dependencies installed with the extras syntax, e.g. from the project root directory:

$ pip install .[test]

tox.ini中为Tox提供相同的语法,无需调整默认install_command:

Give that same syntax to Tox in tox.ini, no need to adjust the default install_command:

[testenv]
commands = {posargs:pytest}
deps = .[test]

现在您不需要在两个位置维护依赖项列表,它们被表示为发布包的位置:在打包元数据而不是 requirements.txt 文件中.

Now you don't need to maintain the dependency list in two places, and they're expressed where they should be for a published package: in the packaging metadata instead of requirements.txt files.

似乎这个额外的小技巧并不少见.

It seems this little extras hack is not all that uncommon.

这篇关于从 setup.py pip 安装 tox 的测试依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆