【问题标题】:I want to make my pytest unittests run with the "python setup.py test" command我想让我的 pytest unittests 使用“python setup.py test”命令运行
【发布时间】:2017-05-11 16:57:51
【问题描述】:

在默认行为中,给出命令python setup.py test 将执行与模块关联的单元测试。只需提供一个测试套件模块列表,就可以使这个默认行为起作用。

替代(但现在已过时)Nose 测试运行器具有类似的功能 - 您只需提供字符串 nose.collector,它使测试命令能够自动发现与项目关联的测试。

但是如果我使用 pytest 呢?似乎没有记录的模式可以从 setup.py 文件运行测试。这是 pytest 库支持的行为吗?

【问题讨论】:

标签: python unit-testing pytest


【解决方案1】:

我个人没有这样做过。但我建议在 github 上查看一些合法的 python 项目,看看他们是如何做到的。 例如。 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'

还可以查看文档:https://docs.pytest.org/en/latest/goodpractices.html#integrating-with-setuptools-python-setup-py-test-pytest-runner(感谢 @jonrsharpe 提供链接)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 2013-06-04
    相关资源
    最近更新 更多