python - 如何在python setup.py test中使用unittest2

标签 python testing setuptools setup.py unittest2

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

最佳答案

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

我的解决方案是用我自己的 test 命令替换默认的 python setup.py 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 命令。

关于python - 如何在python setup.py test中使用unittest2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10100276/

相关文章:

node.js - 找不到磁带测试目录的 Node js 模块

python - 从 setup.py 安装依赖项

python:放置计算机用户可以编辑的应用程序数据的位置

java - 将库从 Java 移植到 Python

python - 使用 OpenCV 在感兴趣区域 (ROI) 周围绘制的线

python - 打包一个要在控制台运行的Python项目 : pyproject. toml文件结构

python-3.x - 使用 setuptools 在 python 包中链接 f2py 生成的 *.so 文件

python - Python-Python是否支持指针?

php - 无法在机器人框架测试中提交表单

java - 测试:无法注册 MBean 实例已经存在