繁体   English   中英

python setup.py 测试中如何使用unittest2

[英]How to use unittest2 in python setup.py test

如何强制python setup.py test使用unittest2 package 进行测试而不是内置的unittest package?

假设您有一个名为tests的目录,其中包含一个__init__.py文件,该文件定义了一个名为 function 的suite ,它返回一个测试套件。

我的解决方案是用我自己的使用unittest2test命令替换默认的python setup.py test命令:

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命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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