您的位置:首页 > 编程语言 > Python开发

python学习笔记(控制语句)

2016-04-06 14:33 555 查看
博主平时学python的时候、大多是复制网上别人现成的进行改动实现自己的测试的要求

所有python基础语法其实掌握的很差

本来想优化下接口脚本实现、发现基础的循环控制语句都不知道怎么写

所以准备整理下

#!/usr/bin/env python
# -*- coding: utf_8 -*-

import requests
import unittest
import re

class Testswcw_back(unittest.TestCase):
def setUp(self):
print "接口测试开始"

def tearDown(self):
print "接口测试结束"

def testlogin_1(self): #登录测试用例
url = 'http://localhost:8081/swcw/back/sysLogin.action'
postparams = {'username':'admin','password':'123456'}
results = requests.post(url,postparams)
pattern = re.compile(r'toMain')
match = pattern.search(results.url)
if results.status_code == 200:
if match != None:
print '用例测试结果:测试通过'
else:
print '用例测试结果:测试失败'
else:
print '用例测试结果:请求失败'

def testlogin_2(self): #登录测试用例
url = 'http://localhost:8081/swcw/back/sysLogin.action'
postparams = {'username':'admin','password':'123457'} #密码错误
results = requests.post(url,postparams)
pattern = re.compile(r'toMain')
match = pattern.search(results.url)
if results.status_code == 200:
if match != None:
print '用例测试结果:测试通过'
else:
print '用例测试结果:测试失败'
else:
print '用例测试结果:请求失败'

def testlogin_3(self): #登录测试用例
url = 'http://localhost:8081/swcw/back/sysLogin.action'
postparams = {'username':'admin1','password':'123456'} #登录名错误
results = requests.post(url,postparams)
pattern = re.compile(r'toMain')
match = pattern.search(results.url)
if results.status_code == 200:
if match != None:
print '用例测试结果:测试通过'
else:
print '用例测试结果:测试失败'
else:
print '用例测试结果:请求失败'

if __name__ == "__main__":
unittest.main()


在原有脚本的基础上添加了控制语句

让输出的结果更清晰

if 语句 嵌套着另一个 if语句

if 条件:

结果

else:

结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: