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

时间:2017-03-21 06:28:35

标签: 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
)

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 ========================

我理解setup.py中的错误确实不是错误,而是py.test运行setup.py而没有任何争论的问题...但我怎样才能避免此错误毁了我的错误测试

1 个答案:

答案 0 :(得分:2)

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

collect_ignore = ['setup.py']

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