Skip unittest test without decorator syntax

后端 未结 4 401
野的像风
野的像风 2021-02-02 14:05

I have a suite of tests that I have loaded using TestLoader\'s (from the unittest module) loadTestsFromModule() method, i.e.,

suite = loader.loadTestsFromModule         


        
4条回答
  •  迷失自我
    2021-02-02 14:32

    Some observations:

    • A test is a callable object with a __call__(result) method
    • TestCase provides a higher-level interface, allowing test methods to throw a SkipTest exception to skip themselves
    • The skip decorators do exactly this
    • Skipped tests are recorded calling the TestResult.addSkip(test, reason) method.

    So you just need to replace the to-be-skipped tests with a custom test that calls addSkip:

    class Skipper(object):
        def __init__(self, test, reason):
            self.test = test
            self.reason = reason
    
        def __call__(self, result):
            result.addSkip(self.test, self.reason)
    

提交回复
热议问题