2012-04-11 54 views

回答

2

讓我們假設你有一個名爲tests目錄中包含一個__init__.py文件,該文件定義了一個名爲suite函數返回一個測試套件。

我的解決方案與使用unittest2我自己test命令替換默認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命令。

+3

但是,使用這個解決方案需要手動安裝'unittest2',因爲'python setup.py test'不再自動從'setup()'''tests_require'列表中安裝軟件包。 – argentpepper 2012-04-12 05:39:54

相關問題