首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python setup.py测试中使用unittest2

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

Stack Overflow用户
提问于 2012-04-11 13:24:43
回答 1查看 1.4K关注 0票数 4

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

EN

回答 1

Stack Overflow用户

发布于 2012-04-12 13:33:28

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

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

代码语言:javascript
复制
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命令。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10100276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档