全局变量评估和装饰器参数

时间:2014-11-10 13:06:27

标签: python evaluation unit-testing

我用unittest进行编码,我有疑问。 我想跳过依赖于全局变量的测试用例。

import unittest

_SKIP_TEST = False
all_suites = unittest.TestSuite()

class Test1(unittest.TestCase):
    @unittest.skipIf(_SKIP_TEST, 'Test should be run')
    def runTest(self):
        global _SKIP_TEST
        _SKIP_TEST = True
        print('Test1 executed')
all_suites.addTest(Test1())

class Test2(unittest.TestCase):
    @unittest.skipIf(_SKIP_TEST, 'Test should skipped')
    def runTest(self):
        print('Test2 executed')
all_suites.addTest(Test2())

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(all_suites

我认为在执行test1(全局变量_SKIP_TEST设置为True)后会跳过test2,但两个测试都已执行。

$ python test.py                                                                        
runTest (__main__.Test1) ... Test1 executed
ok
runTest (__main__.Test2) ... Test2 executed
ok

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

它可能来自可变范围或评估时间问题,或者我不知道的事情.... 如何使这段代码按预期工作?

感谢。

1 个答案:

答案 0 :(得分:1)

装饰器在模块导入时应用;当你将_SKIP_TEST设置为True时装饰器早已应用。

您可以在测试中调用TestCase.skipTest() method

class Test1(unittest.TestCase):
    def runTest(self):
        if _SKIP_TEST:
            self.skipTest(_SKIP_TEST, 'Test should be run')

        global _SKIP_TEST
        _SKIP_TEST = True
        print('Test1 executed')

class Test2(unittest.TestCase):
    def runTest(self):
        if _SKIP_TEST:
            self.skipTest(_SKIP_TEST, 'Test should be skipped')
        print('Test2 executed')

或编写自己的装饰工具,根据可调用的引发unittest.SkipTest() exception

from functools import wraps
from unittest import SkipTest

def dynamicSkipIf(callable, reason):
    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kw):
            if callable():
                raise SkipTest(reason)
            return f(*args, **kw)
        return wrapper
    return decorator

然后将其用作:

class Test1(unittest.TestCase):
    @dynamicSkipIf(lambda: _SKIP_TEST, 'Test should be run')
    def runTest(self):
        global _SKIP_TEST
        _SKIP_TEST = True
        print('Test1 executed')
all_suites.addTest(Test1())

class Test2(unittest.TestCase):
    @dynamicSkipIf(lambda: _SKIP_TEST, 'Test should skipped')
    def runTest(self):
        print('Test2 executed')

执行测试而不是模块导入时,