3 seconds unittest Getting Started

A: unittest run up

unittest use, specific use unittest can view the official website, to be introduced the following simple, most use at work

#
The first step:

create unittest class and must carry unittest.TestCase

class MytestDemo(unittest.TestCase):

 

Step Two:

Create a method that you want to run, and it must be the beginning of a test, unittest is to detect the test will be considered the beginning of the run method, if not the beginning of the test would not

test_show DEF (): 

    Print (ran successfully)

  

The third step:

Run, using the main entry

if __name__ == '__main__':

    unittest.main()

Well now, you are already using the unittest framework

But this is actually at work does not meet us, we will do some pre-conditions before performing the test method, relies on several methods of unittest

 

Second, increase the initialization method,

 

In the class, was added, the setUp (pre-process runs each run method), the tearDown (after the post-processing operation, each method will run running) time setUpClass (Class first run, start operation)

Coding = UTF-8 # 
Import unittest 
Import Requests 

class MytestDemo (unittest.TestCase): 
    '' ' 
    HTTP:? // www.kuaidi100.com / Query of the type = courier company code & postid = express a single 
    test case 
    ps: Express Code: STO = "shentong" 
    EMS = "EMS" 
    SF = "shunfeng" 
    tact = "yuantong" 
    Tong = "zhongtong" 
    rhyme = "yunda" 
    day = "tiantian" 
    Huitong = "huitongkuaidi" 
    full peak = "quanfengkuaidi" 
    Germany state = "debangwuliu" 
    home delivery = "zhaijisong" 
    Number =. 1 
    '' ' 
    @classmethod 
    DEF setUpClass (CLS):
        print ( "The first call when you run the class")

 
    DEF the setUp (Self):
        self.url = "http://www.kuaidi100.com/query"
        self.headers1 = { 'Connection': 'keep-alive'}

    def tearDown(self):
        print("后面收尾")

    def test_yuantong(self):
        url=self.url+"?type=yuantong&postid=11111111111"
        result=requests.get(url=url,headers=self.headers1)
        print(result.url)
        print(result.text)

    def test_tiantian(self):
        data = "type=tiantian&postid=11111111111"
        result = requests.get(url=self.url, params=data,headers=self.headers1 )
        print("************")
        print(result.url)
        print(result.text)
if __name__ == '__main__':
    unittest.main()

  I use two methods, a tact, a day, self.url is to get the url in the setUp, display:

 

 Third, run

3.1 single class run

In the next class method, use, run, a single method

 

3.2: the overall operation

Running in main,

 

In operating mode, unittest.main (), there are several ways to express

Use unittest.TestSuite () operation mode set, this mode can be run in a single class, also you can run multiple files

 

 

 

if __name__ == '__main__':
    #unittest.main()
    suite = unittest.TestSuite()
    suite1 = unittest.TestLoader().loadTestsFromTestCase(MytestDemo)
    # 增加文件
    suite.addTest(suite1)
    unittest.TextTestRunner().run(suite1)

 

 There are several ways to run against TestLoader, class name, file name, run the method name, write a follow-up in

 

Guess you like

Origin www.cnblogs.com/chongyou/p/11458377.html