Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

python - pip install test dependencies for tox from setup.py

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.

I tried this but it did not work:

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

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

With a setup.py like this:

from setuptools import setup

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

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

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

$ pip install .[test]

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...