我试图弄清楚如何让 python setup.py test
运行相当于 python -m unittest discover
。我不想使用 run_tests.py 脚本,也不想使用任何外部测试工具(如 nose
或 py.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/