在使用 setuptools 來(lái)制作python包的安裝程序時(shí),支持使用以下命令來(lái)運(yùn)行測(cè)試用例
python setup.py test
但默認(rèn)情況下是使用 setuptools.command.test:ScanningLoader
來(lái)加載測(cè)試用例
如果要支持使用 unittest discover 來(lái)運(yùn)行測(cè)試用例,需要做一些定制
參考這里的討論
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},
....
)