问题描述
我正在尝试弄清楚如何获取python setup.py test
来运行与python -m unittest discover
等效的内容.我不想使用run_tests.py脚本,也不想使用任何外部测试工具(例如nose
或py.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
目前,我只是使用一个名为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发现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!