python学习笔记(异常处理)
上次提到正则表达式
当未匹配到数据返回值 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学习笔记(异常处理)的更多相关文章
- Python学习笔记(异常处理)
用户输入了一个不合规定的值,或者需要打开的文件不存在.这些情况被称作“异常”,一个好的程序需要能处理可能发生的异常,避免程序因此而中断. 例如我们去打开一个文件: f = file('non-exis ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
- Python学习笔记之异常处理
1.概念 Python 使用异常对象来表示异常状态,并在遇到错误时引发异常.异常对象未被捕获时,程序将终止并显示一条错误信息 >>> 1/0 # Traceback (most re ...
- Python学习笔记进阶篇——总览
Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(Socket编程进阶&多线程.多进程) Python学习笔记——进阶篇[第八周]———进程.线程.协程篇(异常处理) Pyth ...
- python学习笔记目录
人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...
- Python学习笔记之基础篇(-)python介绍与安装
Python学习笔记之基础篇(-)初识python Python的理念:崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. python的历史: 1989年,为了打发圣诞节假期,作者Guido开始写P ...
- Python 学习笔记(下)
Python 学习笔记(下) 这份笔记是我在系统地学习python时记录的,它不能算是一份完整的参考,但里面大都是我觉得比较重要的地方. 目录 Python 学习笔记(下) 函数设计与使用 形参与实参 ...
- 大学四年的Python学习笔记分享之一,内容整理的比较多与仔细
翻到以前在大学坚持记录的Python学习笔记,花了一天的时间整理出来,整理时不经回忆起大学的时光,一眨眼几年就过去了,现在还在上学的你们,一定要珍惜现在,有个充实的校园生活.希望这次的分享对于你们有学 ...
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- VS2013中Python学习笔记[Django Web的第一个网页]
前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...
随机推荐
- IO流入门-第一章-FileInputStream
FileInputStreamj基本用法和方法示例 import java.io.*; public class FileInputStreamTest01 { public static void ...
- vue-cli注册全局组件
在main.js开头引入组件,然后注册组件,例如: import Vue from 'vue' import VueRouter from 'vue-router' import VueResourc ...
- 使用Redis的五个注意事项(命名)
原文:使用Redis的五个注意事项 下面内容来源于Quora上的一个提问,问题是使用Redis需要避免的五个问题.而回答中超出了五个问题的范畴,描述了五个使用Redis的注意事项.如果你在使用或者考虑 ...
- 使用 Python 编写 vim 插件
使用 Python 编写 vim 插件 - 技术翻译 - 开源中国社区 code {margin: 0;padding: 0;white-space: pre;border: none;backgro ...
- CentOS 6.5 QtCreator启动时 dbus-1的错误解决方法
启动QtCreator提示:dbus_connection_can_send_type的错误, QString::arg: Argument missing: 无法解析dbus_connection_ ...
- python安装virtualenv
pip install virtualenv 为了使用更方便用,安装另外一个,windows下要-win,linux下不用 pip install virtualenvwrapper-win 使用方法 ...
- kmp模板 && 扩展kmp模板
kmp模板: #include <bits/stdc++.h> #define PB push_back #define MP make_pair using namespace std; ...
- NGUI,多相机共存时,控制显示的元素
当多个相机存在于一个场景中时,我们需要控制某些元素的可见性来达到我们想要的结果. 1:某个相机不需要看见的元素,统一用一个layer来管理 2:选中UIROOT下的相机 在clear flag里面设置 ...
- 使用阿里的maven库
快使用阿里云的maven仓库 自从开源中国的maven仓库挂了之后就一直在用国外的仓库,慢得想要砸电脑的心都有了.如果你和我一样受够了国外maven仓库的龟速下载?快试试阿里云提供的maven仓库,从 ...
- myelcipse中SVN进行代码更新和提交
感谢博主(John的专栏)的奉献,http://blog.csdn.net/tangzenglei/article/details/50175639