在使用 setuptools 来制作python包的安装程序时,支持使用以下命令来运行测试用例
python setup.py test
但默认情况下是使用 setuptools.command.test:ScanningLoader
来加载测试用例
如果要支持使用 unittest discover 来运行测试用例,需要做一些定制
参考这里的讨论
http://stackoverflow.com/questions/17001010/how-to-run-unittest-discover-from-python-setup-py-test
https://pytest.org/latest/goodpractises.html
from setuptools.command.test import test
def discover_and_run_tests():
import os
import sys
import unittest
# get setup.py directory
setup_file = sys.modules['__main__'].__file__
setup_dir = os.path.abspath(os.path.dirname(setup_file))
# 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
# NOTE: only works for python 2.7 and later
test_suite = test_loader.discover(setup_dir)
# run the test suite
test_runner.run(test_suite)
class DiscoverTest(test):
def finalize_options(self):
test.finalize_options(self)
self.test_args = []
self.test_suite = True
def run_tests(self):
discover_and_run_tests()
setup(
....
cmdclass={'test': DiscoverTest},
....
)
网友评论