python - 如何从 "python setup.py test"运行 unittest discover ?

标签 python unit-testing nose pytest unittest2

我试图弄清楚如何让 python setup.py test 运行相当于 python -m unittest discover。我不想使用 run_tests.py 脚本,也不想使用任何外部测试工具(如 nosepy.test)。如果该解决方案仅适用于 python 2.7 就可以了。

setup.py 中,我想我需要在配置中的 test_suite 和/或 test_loader 字段中添加一些内容,但我可以'似乎找不到正确的组合:

config = {
    'name': name,
    'version': version,
    'url': url,
    'test_suite': '???',
    'test_loader': '???',
}

这是否可能只使用 python 2.7 中内置的 unittest

仅供引用,我的项目结构如下所示:

project/
  package/
    __init__.py
    module.py
  tests/
    __init__.py
    test_module.py
  run_tests.py <- I want to delete this
  setup.py

更新:使用 unittest2 可以做到这一点,但我想只使用 unittest

找到等效的东西

来自 https://pypi.python.org/pypi/unittest2

unittest2 includes a very basic setuptools compatible test collector. Specify test_suite = 'unittest2.collector' in your setup.py. This starts test discovery with the default parameters from the directory containing setup.py, so it is perhaps most useful as an example (see unittest2/collector.py).

目前,我只使用了一个名为 run_tests.py 的脚本,但我希望可以通过转向只使用 python 设置的解决方案来摆脱这个问题。 py 测试.

这是我希望删除的 run_tests.py:

import unittest

if __name__ == '__main__':

    # use the default shared TestLoader instance
    test_loader = unittest.defaultTestLoader

    # use the basic test runner that outputs to sys.stderr
    test_runner = unittest.TextTestRunner()

    # automatically discover all tests in the current dir of the form test*.py
    # NOTE: only works for python 2.7 and later
    test_suite = test_loader.discover('.')

    # run the test suite
    test_runner.run(test_suite)

最佳答案

如果你使用py27+或py32+,解决方法很简单:

test_suite="tests",

关于python - 如何从 "python setup.py test"运行 unittest discover ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17001010/

相关文章:

nose - 解决python中运行unittest的方式太多产生的困惑

python - Emacs + Rope + Python 产生 lisp 错误

python - 使用 odeint 求解带有阶跃函数参数的 ODE 集

c# - 多个 Moq It.Is<string>() 匹配参数

sql-server - 如何对持久性进行单元测试?

python - 在脚本中使用 Python 中的 nose 时覆盖现有配置

python - 如何在 Nose 下执行 "early return"导入?

python - wsgi如何处理同名的多个请求头?

python - 下载并安装 scapy for windows for python 3.5?

node.js - 使用 context.succeed() 的 Node 单元测试 AWS Lambda 函数