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

前端 未结 7 2061
不知归路
不知归路 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条回答
  •  -上瘾入骨i
    2020-12-12 20:01

    Another less than ideal solution slightly inspired by http://hg.python.org/unittest2/file/2b6411b9a838/unittest2/collector.py

    Add a module that returns a TestSuite of discovered tests. Then configure setup to call that module.

    project/
      package/
        __init__.py
        module.py
      tests/
        __init__.py
        test_module.py
      discover_tests.py
      setup.py
    

    Here's discover_tests.py:

    import os
    import sys
    import unittest
    
    def additional_tests():
        setup_file = sys.modules['__main__'].__file__
        setup_dir = os.path.abspath(os.path.dirname(setup_file))
        return unittest.defaultTestLoader.discover(setup_dir)
    

    And here's setup.py:

    try:
        from setuptools import setup
    except ImportError:
        from distutils.core import setup
    
    config = {
        'name': 'name',
        'version': 'version',
        'url': 'http://example.com',
        'test_suite': 'discover_tests',
    }
    
    setup(**config)
    

提交回复
热议问题