我想让我的 pytest 单元测试与“python setup.py test"一起运行命令 [英] I want to make my pytest unittests run with the "python setup.py test" command
问题描述
在给出命令 python setup.py test
的默认行为中,将执行与模块关联的单元测试.只需提供测试套件模块列表,即可使此默认行为生效.
In the default behaviour giving the command python setup.py test
will execute the unit tests associated with a module. This default behaviour can be made to work simply by providing a list of test-suite modules.
替代(但现在已过时)Nose 测试运行器具有类似的功能 - 您只需提供字符串 nose.collector
,它使测试命令能够自动发现与项目.
The alternative (but now obsolete) Nose test runner has a comparable feature - you can just provide the string nose.collector
which gives the test command the ability to auto-discover the tests associated with the project.
但是如果我使用 pytest 呢?似乎没有记录模式可以从 setup.py 文件运行测试.这是 pytest 库支持的行为吗?
But what if I'm using pytest? There doesn't seem to be a documented pattern to run tests from the setup.py file. Is this a behaviour supported by the pytest library?
推荐答案
我个人还没有这样做过.但我建议在 github 上查看一些合法的 python 项目,看看他们是如何做到这一点的.例如.请求:
I personally have not done this. But I'd recommend checking out some legit python projects on github to see how they have done this. E.g. requests:
#!/usr/bin/env python
import sys
from setuptools import setup
from setuptools.command.test import test as TestCommand
class PyTest(TestCommand):
user_options = [('pytest-args=', 'a', "Arguments to pass into py.test")]
def initialize_options(self):
TestCommand.initialize_options(self)
self.pytest_args = []
def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = []
self.test_suite = True
def run_tests(self):
import pytest
errno = pytest.main(self.pytest_args)
sys.exit(errno)
setup(
# pass all the required/desired args
cmdclass={'test': PyTest}
)
您甚至可以将 args 传递给 pytest runner python setup.py test --pytest-args='-vvv'
You can even pass args to pytest runner python setup.py test --pytest-args='-vvv'
还可以查看文档:https://docs.pytest.org/en/latest/goodpractices.html#integrating-with-setuptools-python-setup-py-test-pytest-runner(感谢@jonrsharpe链接)
Also checkout the docs: https://docs.pytest.org/en/latest/goodpractices.html#integrating-with-setuptools-python-setup-py-test-pytest-runner (thanks to @jonrsharpe for the link)
这篇关于我想让我的 pytest 单元测试与“python setup.py test"一起运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!