【问题标题】:Disable a Python unit test with a message使用消息禁用 Python 单元测试
【发布时间】:2014-03-31 08:05:25
【问题描述】:

我的Python版本不支持

    @unittest.skip("demonstrating skipping")

来自Disable individual Python unit tests temporarily,我了解如何使用装饰器来实现这一点,即,

def disabled(f):
    def _decorator():
        print f.__name__ + ' has been disabled'
    return _decorator

@disabled
def testFoo():
    '''Foo test case'''
    print 'this is foo test case'

testFoo()

但是,装饰器不支持为跳过提供消息。请问我可以知道如何实现这一目标吗?我基本上想要类似的东西

def disabled(f, msg):
    def _decorator():
        print f.__name__ + ' has been disabled' + msg
    return _decorator

@disabled("I want to skip it")
def testFoo():
    '''Foo test case'''
    print 'this is foo test case'

testFoo()

【问题讨论】:

    标签: python unit-testing


    【解决方案1】:

    你可以像这样修改装饰器:

    def disabled(msg):
        def _decorator(f):
            def _wrapper():
                print f.__name__ + ' has been disabled ' + msg
            return _wrapper
        return _decorator
    
    
    @disabled("I want to skip it")
    def testFoo():
        '''Foo test case'''
        print 'this is foo test case'
    
    
    testFoo()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-17
      • 2013-09-10
      • 2011-01-05
      • 2011-07-19
      • 1970-01-01
      • 2017-10-24
      • 2011-06-30
      • 1970-01-01
      相关资源
      最近更新 更多