如何使用 py.test 在 setup.py 上避免“未提供命令”

iNy*_*yar 6 python pytest

我已经开始了一个小的 Python 包(基于python-telegram-bot),并且想做基于测试的开发。所以我py.test用一些基本的单元测试来激活。

但是我总是收到错误消息,setup.py因为它需要一个命令作为参数,但py.test不提供任何命令。

这是 的缩短版本setup.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from setuptools import setup

requirements = [
    'python-telegram-bot>=5.3.0'
]

test_requirements = [
    'pytest>=3.0'
]

setup(
    name='project',
    version='0.1.0',
    packages=[
        'project',
    ],
    package_dir={'project':
                 'project'},
    entry_points={
        'console_scripts': [
            'project=project.cli:main'
        ]
    },
    include_package_data=True,
    install_requires=requirements,
    license="MIT license",
    zip_safe=False,
    test_suite='tests',
    tests_require=test_requirements
)
Run Code Online (Sandbox Code Playgroud)

py.test结果:

=========================== test session starts ==========================
platform darwin -- Python 3.6.0, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
rootdir: ~/dev/project, inifile: pytest.ini
collected 4 items / 1 errors
================================= ERRORS =================================
________________________ ERROR collecting setup.py _______________________
/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/core.py:134: in setup
    ok = dist.parse_command_line()
../../../.virtualenvs/project/lib/python3.6/site-packages/setuptools/dist.py:358: in parse_command_line
    result = _Distribution.parse_command_line(self)
/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/dist.py:490: in parse_command_line
    raise DistutilsArgError("no commands supplied")
E   distutils.errors.DistutilsArgError: no commands supplied

During handling of the above exception, another exception occurred:
setup.py:58: in <module>
    tests_require=test_requirements
/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/core.py:136: in setup
    raise SystemExit(gen_usage(dist.script_name) + "\nerror: %s" % msg)
E   SystemExit: usage: py.test [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
E      or: py.test --help [cmd1 cmd2 ...]
E      or: py.test --help-commands
E      or: py.test cmd --help
E
E   error: no commands supplied
!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!
========================= 1 error in 1.04 seconds ========================
Run Code Online (Sandbox Code Playgroud)

据我所知,它是不是真的一个错误setup.py,而是一个有问题的py.test运行setup.py没有任何说法......但我怎么能避免这个错误毁了我的测试?

Igo*_*rol 6

conftest.py使用以下文本将文件添加到根目录:

collect_ignore = ['setup.py']
Run Code Online (Sandbox Code Playgroud)

它会告诉 py.test 忽略setup.py.