Python语言异常处理测试过程解析
作者:sowhat1943 时间:2022-10-30 01:29:05
这篇文章主要介绍了Python语言异常处理测试过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
(一)异常处理
1.捕获所有异常
try:
x = 5 / 0
except:
print('程序有错误')
2.捕获特定异常
try:
x = 5 / 0
except ZeroDivisionError as e:
print('不能为0',e)
except:
print('其他错误')
else:
print('没有错误')
finally:
print('关闭资源')
3.手动抛出异常
def method():
raise NotImplementedError('该方法还未被实现')
(二)测试
使用Python自带的unittest模块
example 1:测试某个函数
import unittest
from example import get_formatted_name
class NameTestCase(unittest.TestCase):
def test_title_name(self):
formatted_name = get_formatted_name('tom','lee')
self.assertEqual(formatted_name,'Tom Lee')
if __name__ == '__main__':
unittest.main()
example 2:测试某个类
class Coder:
def __init__(self,name):
self.name = name
self.skills = []
def mastering_skill(self,skill):
self.skills.append(skill)
def show_skills(self):
print('掌握技能:')
for skill in self.skills:
print('-',skill)
import unittest
from coder import Coder
class CoderTestCase(unittest.TestCase):
def setUp(self):
self.c = Coder('Tom')
self.c.mastering_skill('Python')
self.c.mastering_skill('Java')
def test_skill_in(self):
self.assertIn("Python",self.c.skills)
def tearDown(self):
print('销毁')
if __name__ == '__main__':
unittest.main()
常用的断言方法:
import unittest
person ={'name':'Tom','age':30}
numbers = [1,23,3,4,4,54]
s = 'hello world python'
class TestAssert(unittest.TestCase):
def test_assert_method(self):
self.assertEqual('Tom',person.get('name'))
self.assertTrue('hello' in s)
self.assertIn('hello',s)
#self.assertEqual(3.3,1.1+2.2)
self.assertAlmostEqual(3.3,1.1+2.2)
#判断在内存中是否是同一个引用
self.assertIs(True+1,2)
self.assertIsNone(None)
#判断是否是某个类型的实例
self.assertIsInstance(numbers[0],int)
#是否大于
self.assertGreater(5,4)
if __name__ == '__main__':
unittest.main()
来源:https://www.cnblogs.com/gdy1993/p/12154049.html
标签:Python,异常,处理,测试
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python字符串三种格式化输出
2022-12-15 09:47:59
淘宝CSS框架研究(1):Reset CSS(八卦篇)
2009-03-31 12:58:00
python模块之paramiko实例代码
2022-08-08 08:34:51
30分钟就入门的正则表达式基础教程
2024-05-13 10:37:40
javascript计时器事件使用详解
2024-05-08 09:38:58
python根据京东商品url获取产品价格
2022-01-26 12:14:31
Python使用pytest-playwright的原因分析
2023-12-12 01:22:39
sql2005 远程连接问题解决方法
2024-01-19 13:37:42
uniapp小视频项目开发之滑动播放视频
2023-07-02 05:24:36
![](https://img.aspxhome.com/file/2023/3/137033_0s.jpg)
Python还能这么玩之只用30行代码从excel提取个人值班表
2022-05-03 22:49:45
![](https://img.aspxhome.com/file/2023/7/133517_0s.png)
Python深入浅出分析元类
2022-01-10 09:09:00
Python字符串拼接的几种方法整理
2021-05-08 18:55:47
![](https://img.aspxhome.com/file/2023/5/64735_0s.jpg)
关于Vue的异步组件
2024-05-09 10:52:35
解决Tensorflow 使用时cpu编译不支持警告的问题
2023-12-30 07:28:01
![](https://img.aspxhome.com/file/2023/0/70780_0s.jpg)
mysql-8.0.15-winx64 使用zip包进行安装及服务启动后立即关闭问题
2024-01-23 05:58:37
![](https://img.aspxhome.com/file/2023/9/108869_0s.jpg)
可刷新的Div+CSS+JS二级下拉树型菜单
2007-10-09 19:14:00
SQL Server的怪辟:异常与孤立事务
2009-09-24 14:11:00
解读ASP.NET 5 & MVC6系列教程(2):初识项目
2023-06-28 02:10:47
![](https://img.aspxhome.com/file/2023/8/75528_0s.png)
python提取word文件中的图片并上传阿里云OSS
2021-02-01 10:52:25
![](https://img.aspxhome.com/file/2023/4/107964_0s.png)
SQL Server 在分页获取数据的同时获取到总记录数
2024-01-24 09:04:13