How to run unittest discover from “python setup.py test”?

前端 未结 7 2077
不知归路
不知归路 2020-12-12 19:39

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.p

7条回答
  •  佛祖请我去吃肉
    2020-12-12 20:11

    From Building and Distributing Packages with Setuptools (emphasis mine):

    test_suite

    A string naming a unittest.TestCase subclass (or a package or module containing one or more of them, or a method of such a subclass), or naming a function that can be called with no arguments and returns a unittest.TestSuite.

    Hence, in setup.py you would add a function that returns a TestSuite:

    import unittest
    def my_test_suite():
        test_loader = unittest.TestLoader()
        test_suite = test_loader.discover('tests', pattern='test_*.py')
        return test_suite
    

    Then, you would specify the command setup as follows:

    setup(
        ...
        test_suite='setup.my_test_suite',
        ...
    )
    

提交回复
热议问题