pip install test dependencies for tox from setup.py

前端 未结 4 969
你的背包
你的背包 2020-12-16 09:57

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 (

4条回答
  •  轮回少年
    2020-12-16 10:34

    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.

提交回复
热议问题