上次提到正则表达式

当未匹配到数据返回值 None

再使用 match.group 会出现异常 AttributeError

为了避免异常我改成“ match != None”

这次加入异常处理

 #!/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':''}
results = requests.post(url,postparams)
pattern = re.compile(r'toMain')
match = pattern.search(results.url)
try:
if results.status_code == 200:
if match.group() == 'toMain':
print '用例测试结果:测试通过'
else:
print '用例测试结果:请求失败'
except AttributeError:
print '用例测试结果:测试失败' def testlogin_2(self): #登录测试用例
url = 'http://localhost:8081/swcw/back/sysLogin.action'
postparams = {'username':'admin','password':''} #密码错误
results = requests.post(url,postparams)
pattern = re.compile(r'toMain')
match = pattern.search(results.url)
try:
if results.status_code == 200:
if match.group() == 'toMain':
print '用例测试结果:测试通过'
else:
print '用例测试结果:请求失败'
except AttributeError:
print '用例测试结果:测试失败' def testlogin_3(self): #登录测试用例
url = 'http://localhost:8081/swcw/back/sysLogin.action'
postparams = {'username':'admin1','password':''} #登录名错误
results = requests.post(url,postparams)
pattern = re.compile(r'toMain')
match = pattern.search(results.url)
try:
if results.status_code == 200:
if match.group() == 'toMain':
print '用例测试结果:测试通过'
else:
print '用例测试结果:请求失败'
except AttributeError:
print '用例测试结果:测试失败' if __name__ == "__main__":
unittest.main()

异常处理的格式:

try:

  【代码】

except 【异常名称】:

  【代码】

如果想屏蔽所有异常 如下即可

try:

  【代码】

except:

  【代码】

python学习笔记(异常处理)的更多相关文章

  1. Python学习笔记(异常处理)

    用户输入了一个不合规定的值,或者需要打开的文件不存在.这些情况被称作“异常”,一个好的程序需要能处理可能发生的异常,避免程序因此而中断. 例如我们去打开一个文件: f = file('non-exis ...

  2. python学习笔记(六)文件夹遍历,异常处理

    python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...

  3. Python学习笔记之异常处理

    1.概念 Python 使用异常对象来表示异常状态,并在遇到错误时引发异常.异常对象未被捕获时,程序将终止并显示一条错误信息 >>> 1/0 # Traceback (most re ...

  4. Python学习笔记进阶篇——总览

    Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(Socket编程进阶&多线程.多进程) Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(异常处理) Pyth ...

  5. python学习笔记目录

    人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...

  6. Python学习笔记之基础篇(-)python介绍与安装

    Python学习笔记之基础篇(-)初识python Python的理念:崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. python的历史: 1989年,为了打发圣诞节假期,作者Guido开始写P ...

  7. Python 学习笔记(下)

    Python 学习笔记(下) 这份笔记是我在系统地学习python时记录的,它不能算是一份完整的参考,但里面大都是我觉得比较重要的地方. 目录 Python 学习笔记(下) 函数设计与使用 形参与实参 ...

  8. 大学四年的Python学习笔记分享之一,内容整理的比较多与仔细

    翻到以前在大学坚持记录的Python学习笔记,花了一天的时间整理出来,整理时不经回忆起大学的时光,一眨眼几年就过去了,现在还在上学的你们,一定要珍惜现在,有个充实的校园生活.希望这次的分享对于你们有学 ...

  9. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  10. VS2013中Python学习笔记[Django Web的第一个网页]

    前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...

随机推荐

  1. Design Pattern – Proxy, Adapter, Facade, Mediator

    这几个模式比较类似, 都是用作interface, 但有所不同 Proxy, 特点是以假乱真, client在使用的时候就和在使用真正的object一样, 接口完全一致, proxy和object的交 ...

  2. Python菜鸟之路:Django 序列化数据

    类型一:对于表单数据进行序列化 这时需要用到ErrorDict. ret['errors'] = obj.errors.as_data() result = json.dumps(ret, cls=J ...

  3. DOM 综合练习(二)

    // 需求一: 二级联动菜单 <html> <head> <style type="text/css"> select{ width:100px ...

  4. 四.mysql演示银行转账

    代码演示: #conding:utf8 import pymysql import sys class TransferMoney(object): def __init__(self,conn): ...

  5. 服务器(Ubuntu)远程访问ipython notebook(服务器运行ipython notebook 本地浏览器访问)

    准备工作 首先要安装 ipython 推荐直接 Anaconda 搞起(装在服务器). Anaconda 帮你集成N多python相关环境(包),省得你再手动咔咔一顿安装 服务器启动ipython n ...

  6. 【我的Android进阶之旅】解决 Error:CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版本不兼容。请查看计算机的系统信息,了解是否需要 x86

    一.错误描述 刚刚打开Android Studio新建一个项目,然后就编译不了,报了如下所示的错误: 错误描述为: Error:CreateProcess error=216, 该版本的 %1 与您运 ...

  7. Q35+uefi or bios+legacy // PCI | PCIE

    1:首先统一可扩展固件接口(UEFI)是一种规范定义操作系统和平台固件之间的软件接口. UEFI旨在替代基本输入/输出系统(BIOS)固件接口.(legacy) 硬件平台厂商越来越多地采用UEFI管理 ...

  8. [intellij]create gradle project

    https://intellij-support.jetbrains.com/hc/en-us/community/posts/206806425/comments/206797339

  9. appium的初始化准备工作

    文章出处http://blog.csdn.net/jiuzuidongpo/article/details/51790455 Appium在接收到客户端脚本的连接之后的初始化准备工作列表(细节部分详细 ...

  10. 关于OAuth的state参数的作用

    引用: https://blog.csdn.net/gjb724332682/article/details/54428808 在开发 OAuth认证服务器 的时候,开发者的安全意识不高的话,很可能会 ...