Skip unittest --- unittest use cases

  We are doing automated testing, you may encounter some use cases do not return to the middle, you want to be skipped. Direct comments, then modify the code amount is too large, this approach is clearly wrong, so what's the method? unittest this automation framework can help us to complete this operation

Example automatically skipped with

unittest provides some decorator Skips use cases. We can help us to complete these actions by these decorators

@unittest.skip()

He represents: Use Case skipped unconditionally

def skip(reason):
    """
   无条件地跳过用例
    """
    def decorator(test_item):
        if not isinstance(test_item, type):
            @functools.wraps(test_item)
            def skip_wrapper(*args, **kwargs):
                raise SkipTest(reason)
            test_item = skip_wrapper

        test_item.__unittest_skip__ = True
        test_item.__unittest_skip_why__ = reason
        return test_item
    return decorator

@unittest.skipIf()

He said: If the condition is true, then skip the test.

DEF SKIPIF (for condition Condition, reason):
     "" " 
    If the condition is true, the test is skipped. " 
    "" 
    IF for condition Condition:
         return Skip (reason)
     return the _id

@ Unittest.skipUnless ()

He said: Unless the condition is true, otherwise skip the test.

DEF skipUnless (for condition Condition, reason):
     "" " 
   Unless the condition is true, otherwise skip the test. " 
    "" 
    IF  not for condition Condition:
         return Skip (reason)
     return _id

 

Small scale chopper

Without further ado begin direct combat, where we use the same interface to complete our test, of course, interface documentation, or our inquiry songs interfaces (simple, free, better use)

# coding:utf-8
import unittest
import requests

class   Music(unittest.TestCase):

    def select(self,name):
        url = 'https://api.apiopen.top/searchMusic'
        data = {
             "name":name
        }
        r = requests.post(url,data=data)
        b = r.json()['result'][0]['title']
        return b

    @unittest.skip("Mandatory skip " )
     DEF Test01 (Self): 
        B = ' Broken Bridge ' 
        A = self.select (B) 
        self.assertEqual (B, A) 
        Print ( " used in Example 1 skip " ) 

    @ unittest.skipIf (True , ' condition is true skip ' )
     DEF Test02 (Self): 
        A = ' that good cry ' 
        B = self.select (A) 
        self.assertEqual (A, B) 
        Print ( " using Example 2 skip " )

    unittest.skipUnless @ (False, ' condition is false when skipping ' )
     DEF TEST03 (Self): 
        A = ' Mangzhong ' 
        C = ' vibrato ' 
        B = self.select (A)
         the try : 
            self.assertIn (C, B, MSG = ' \ n-vibrato song information is not present in Ear ' )
         the except Exception AS MSG:
             Print ( ' error message S% ' % MSG)
         Print ( " with Example 3 skip " ) 

    DEFtest04 (Self): 
        A = ' like fish ' 
        B = self.select (A) 
        self.assertEqual (A, B) 
        Print ( " used in Example 4 not Skip " ) 

IF  the __name__ == ' __main__ ' : 
    unittest.main (the verbosity = 2)

We can see by the code with Example 123 was skipped, but skipping is different for each condition, after performing found only in Example 4 with no skipping, of course, we are only the code for use in Example 4 without skipping.

Can also be seen from the results, we skipped a total of three use cases, and each is how to be skipped

There are little friends to ask, just skip this single use case, then I want to skip all the use cases in a class of it? This is not simple? Directly to the decorator on the class above is not on it.

# coding:utf-8
import unittest
import requests

@unittest.skip('强制性跳过')
class   Music(unittest.TestCase):

    def select(self,name):
        url = 'https://api.apiopen.top/searchMusic'
        data = {
             "name":name
        }
        r = requests.post(url,data=data)
        b = r.json()['result'][0]['title']
        return B 

    DEF Test01 (Self): 
        B = ' Broken Bridge ' 
        A = self.select (B) 
        self.assertEqual (B, A) 
        Print ( " used in Example 1 skip " ) 

    DEF Test02 (Self): 
        A = ' said good cry ' 
        B = self.select (A) 
        self.assertEqual (A, B) 
        Print ( " using Example 2 skip " ) 

    DEF TEST03 (Self): 
        A = ' Mangzhong ' 
        C =' Vibrato ' 
        B = self.select (A)
         the try : 
            self.assertIn (C, B, MSG = ' \ n-vibrato song information is not present in Ear ' )
         the except Exception AS MSG:
             Print ( ' error message S% ' % MSG)
         Print ( " with Example 3 skip " ) 

    DEF test04 (Self): 
        A = ' like fish ' 
        B = self.select (A) 
        self.assertEqual (A, B) 
        Print ( " use not skip Example 4")

if __name__ == '__main__':  
    unittest.main(verbosity=2)

By performing it has been found that the class of the embodiment with four all skipped.

 

 

 

Quiet write articles if it helps you, you can focus on a point, where if there is a mistake or do not know where you can leave a message below, the first time after seeing the reply!

 

 

Guess you like

Origin www.cnblogs.com/qican/p/11868674.html