python学习笔记(控制语句)

博主平时学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:

   结果

原文地址:https://www.cnblogs.com/cllovewxq/p/5359256.html