Как использовать unittest2 в тесте python setup.py

Как я могу заставить python setup.py test использовать unittest2 пакет для тестирования вместо встроенного unittest пакет?

1 ответ

Предположим, у вас есть каталог с именем tests который содержит __init__.py файл, который определяет функцию с именем suite который возвращает набор тестов.

Мое решение состоит в том, чтобы заменить по умолчанию python setup.py test команда с моим собственным test команда, которая использует 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}
)

Сейчас работает python setup.py test работает мой обычай test команда.

Другие вопросы по тегам