如何从"python setup.py test"运行unittest发现? [英] How to run unittest discover from "python setup.py test"?

查看:120
本文介绍了如何从"python setup.py test"运行unittest发现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何获取python setup.py test来运行与python -m unittest discover等效的内容.我不想使用run_tests.py脚本,也不想使用任何外部测试工具(例如nosepy.test).如果该解决方案仅适用于python 2.7,就可以了.

I'm trying to figure out how to get python setup.py test to run the equivalent of python -m unittest discover. I don't want to use a run_tests.py script and I don't want to use any external test tools (like nose or py.test). It's OK if the solution only works on python 2.7.

setup.py中,我认为我需要在config的test_suite和/或test_loader字段中添加一些内容,但是我似乎找不到能正常工作的组合:

In setup.py, I think I need to add something to the test_suite and/or test_loader fields in config, but I can't seem to find a combination that works correctly:

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

仅使用python 2.7内置的unittest可以实现吗?

Is this possible using only unittest built into python 2.7?

仅供参考,我的项目结构如下:

FYI, my project structure looks like this:

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

更新:对于unittest2,这是可能的,但我想仅使用unittest

Update: This is possible with unittest2 but I want find something equivalent using only unittest

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

unittest2包括一个非常基本的setuptools兼容测试收集器.在setup.py中指定test_suite ='unittest2.collector'.这将从包含setup.py的目录中的默认参数开始测试发现,因此它可能最有用作为示例(请参阅unittest2/collector.py).

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 setup.py test的解决方案来摆脱这种情况.

For now, I'm just using a script called run_tests.py, but I'm hoping I can get rid of this by moving to a solution that only uses python setup.py test.

这是我希望删除的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 +,则解决方案非常简单:

If you use py27+ or py32+, the solution is pretty simple:

test_suite="tests",

这篇关于如何从"python setup.py test"运行unittest发现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆