禁用Python单元测试并附带消息

-1 投票
1 回答
529 浏览
提问于 2025-04-17 20:20

我用的Python版本不支持

    @unittest.skip("demonstrating skipping")

来自 暂时禁用单个Python单元测试,我知道怎么用装饰器来实现这个功能,也就是

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

你可以这样修改装饰器:

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

撰写回答