简体   繁体   中英

How to use unittest2 in python setup.py test

How can I force python setup.py test to use the unittest2 package for testing instead of the built-in unittest package?

Let's assume you have a directory called tests which contains an __init__.py file which defines a function called suite which returns a test suite.

My solution is to replace the default python setup.py test command with my own test command which uses unittest2 :

from setuptools import Command
from setuptools import setup

class run_tests(Command):
    """Runs the test suite using the ``unittest2`` package instead of the     
    built-in ``unittest`` package.                                            

    This is necessary to override the default behavior of ``python setup.py   
    test``.                                                                   

    """
    #: A brief description of the command.                                    
    description = "Run the test suite (using unittest2)."

    #: Options which can be provided by the user.                             
    user_options = []

    def initialize_options(self):
        """Intentionally unimplemented."""
        pass

    def finalize_options(self):
        """Intentionally unimplemented."""
        pass

    def run(self):
        """Runs :func:`unittest2.main`, which runs the full test suite using  
        ``unittest2`` instead of the built-in :mod:`unittest` module.         

        """
        from unittest2 import main
        # I don't know why this works. These arguments are undocumented.      
        return main(module='tests', defaultTest='suite',
                    argv=['tests.__init__'])

setup(
  name='myproject',
  ...,
  cmd_class={'test': run_tests}
)

Now running python setup.py test runs my custom test command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM