標籤:
博主平時學python的時候、大多是複製網上別人現成的進行改動實現自己的測試的要求
所有python基礎文法其實掌握的很差
本來想最佳化下介面指令碼實現、發現基礎的迴圈控制語句都不知道怎麼寫
所以準備整理下
1 #!/usr/bin/env python 2 # -*- coding: utf_8 -*- 3 4 import requests 5 import unittest 6 import re 7 8 class Testswcw_back(unittest.TestCase): 9 def setUp(self):10 print "介面測試開始"11 12 def tearDown(self):13 print "介面測試結束"14 15 def testlogin_1(self): #登入測試案例16 url = ‘http://localhost:8081/swcw/back/sysLogin.action‘17 postparams = {‘username‘:‘admin‘,‘password‘:‘123456‘}18 results = requests.post(url,postparams)19 pattern = re.compile(r‘toMain‘)20 match = pattern.search(results.url)21 if results.status_code == 200:22 if match != None:23 print ‘用例測試結果:測試通過‘24 else:25 print ‘用例測試結果:測試失敗‘26 else:27 print ‘用例測試結果:請求失敗‘28 29 def testlogin_2(self): #登入測試案例30 url = ‘http://localhost:8081/swcw/back/sysLogin.action‘31 postparams = {‘username‘:‘admin‘,‘password‘:‘123457‘} #密碼錯誤32 results = requests.post(url,postparams)33 pattern = re.compile(r‘toMain‘)34 match = pattern.search(results.url)35 if results.status_code == 200:36 if match != None:37 print ‘用例測試結果:測試通過‘38 else:39 print ‘用例測試結果:測試失敗‘40 else:41 print ‘用例測試結果:請求失敗‘42 43 def testlogin_3(self): #登入測試案例44 url = ‘http://localhost:8081/swcw/back/sysLogin.action‘45 postparams = {‘username‘:‘admin1‘,‘password‘:‘123456‘} #登入名稱錯誤46 results = requests.post(url,postparams)47 pattern = re.compile(r‘toMain‘)48 match = pattern.search(results.url)49 if results.status_code == 200:50 if match != None:51 print ‘用例測試結果:測試通過‘52 else:53 print ‘用例測試結果:測試失敗‘54 else:55 print ‘用例測試結果:請求失敗‘56 57 if __name__ == "__main__":58 unittest.main()
在原有指令碼的基礎上添加了控制語句
讓輸出的結果更清晰
if 語句 嵌套著另一個 if語句
if 條件:
結果
else:
結果
python學習筆記(控制語句)