使用消息禁用python单元测试

时间:2014-02-27 17:42:38

标签: python unit-testing

我的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()

1 个答案:

答案 0 :(得分:0)

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

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()