我试图弄清楚如何python setup.py test
运行相当于python -m unittest discover
. 我不想使用 run_tests.py 脚本,也不想使用任何外部测试工具(如nose
or py.test
)。如果该解决方案仅适用于 python 2.7 就可以了。
在setup.py
中,我认为我需要在配置中的test_suite
和/或test_loader
字段中添加一些内容,但我似乎找不到正确工作的组合:
config = {
'name': name,
'version': version,
'url': url,
'test_suite': '???',
'test_loader': '???',
}
这是否可能仅使用unittest
内置于 python 2.7 中?
仅供参考,我的项目结构如下所示:
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 包括一个非常基本的 setuptools 兼容测试收集器。在 setup.py 中指定 test_suite = 'unittest2.collector'。这将使用包含 setup.py 的目录中的默认参数启动测试发现,因此它可能是最有用的示例(请参阅 unittest2/collector.py)。
目前,我只使用了一个名为 的脚本run_tests.py
,但我希望通过转向仅使用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)