python中unittest框架应用详解
更新时间:2021年9月18日 08:06 点击:1868
1、Unittest为Python内嵌的测试框架,不需要特殊配置
2、编写规范
需要导入 import unittest
测试类必须继承unittest.TestCase
测试方法以 test_开头
模块和类名没有要求
TestCase
理解为写测试用例
TestSuite
理解为测试用例的集合
TestLoader
理解为的测试用例加载
TestRunner
执行测试用例,并输出报告
import unittest from class_api_login_topup.demo import http_request from class_api_login_topup.http_attr import Get_Attr # 反射的值 获取 cookies # 这是文件http_attr中的Get_Attr类 class Get_Attr: cookies = None class Login_Http(unittest.TestCase): def __init__(self, methodName, url, data, method, expected): super(Login_Http, self).__init__(methodName) # 超继承 self.url = url self.data = data self.expected = expected self.method = method def test_api(self): # 正常登录 res = http_request().request(self.url, self.data, self.method, getattr(Get_Attr, 'cookies')) if res.cookies: setattr(Get_Attr, 'cookies', res.cookies) try: self.assertEqual(self.expected, res.json()['code']) except AssertionError as e: print("test_api's, error is {0}", format(e)) raise e print(res.json()) if __name__ == '__main__': unittest.main()
执行一:
import unittest from class_demo_login_topup.http_tools import Login_Http suite = unittest.TestSuite() loader = unittest.TestLoader() test_data = [{'url': 'http://test.lemonban.com/futureloan/mvc/api/member/login', 'data': {'mobilephone': 'xxxx', 'pwd': '123456'}, 'expected': '10001', 'method': 'get'}, {'url': 'http://test.lemonban.com/futureloan/mvc/api/member/login', 'data': {'mobilephone': 'xxxx', 'pwd': '12345678'}, 'expected': '20111', 'method': 'get'}, {'url': 'http://test.lemonban.com/futureloan/mvc/api/member/recharge', 'data': {'mobilephone': 'xxxx', 'amount': '1000'}, 'expected': '10001', 'method': 'post'}, {'url': 'http://test.lemonban.com/futureloan/mvc/api/member/recharge', 'data': {'mobilephone': 'xxxx', 'amount': '-100'}, 'expected': '20117', 'method': 'post'}] # 遍历数据,执行脚本 addTest 单个执行 for item in test_data: suite.addTest(Login_Http('test_api', item['url'], item['data'], item['method'], item['expected'])) # 执行 with open('http_TestCase.txt', 'w+', encoding='UTF-8') as file: runner = unittest.TextTestRunner(stream=file, verbosity=2) runner.run(suite) # 运行结果 {'status': 1, 'code': '10001', 'data': None, 'msg': '登录成功'} {'status': 0, 'code': '20111', 'data': None, 'msg': '用户名或密码错误'} {'status': 1, 'code': '10001', 'data': {'id': 10011655, 'regname': '小蜜蜂', 'pwd': 'E10ADC3949BA59ABBE56E057F20F883E', 'mobilephone': 'xxxx', 'leaveamount': '150000.00', 'type': '1', 'regtime': '2021-07-14 14:54:08.0'}, 'msg': '充值成功'} {'status': 0, 'code': '20117', 'data': None, 'msg': '请输入范围在0到50万之间的正数金额'}
执行二:把test_data的数据放在EXCEL中运行。
import unittest from class_demo_login_topup.http_tools import Login_Http suite = unittest.TestSuite() loader = unittest.TestLoader() test_data = HttpExcel('test_api.xlsx', 'python').real_excel() for item in test_data: suite.addTest(Login_Http('test_api', item['url'], eval(item['data']), item['method'], str(item['expected']))) with open('http_TestCase.txt', 'w+', encoding='UTF-8') as file: runner = unittest.TextTestRunner(stream=file, verbosity=2) runner.run(suite)
执行三、直接用装饰器ddt
import unittest from class_api_login_topup.demo import http_request from class_api_login_topup.http_attr import Get_Attr # 反射的值 from ddt import ddt, data, unpack from class_demo_login_topup.http_excel import HttpExcel test_data = HttpExcel('test_api.xlsx', 'python').real_excel() @ddt class Login_Http(unittest.TestCase): @data(*test_data) def test_api(self, item): # 正常登录 res = http_request().request(item['url'], eval(item['data']), item['method'], getattr(Get_Attr, 'cookies')) if res.cookies: setattr(Get_Attr, 'cookies', res.cookies) try: self.assertEqual(str(item['expected']), res.json()['code']) except AssertionError as e: print("test_api's, error is {0}", format(e)) raise e print(res.json())
执行ddt方式一
import unittest from class_demo_login_topup.http_tools import Login_Http from class_demo_login_topup.http_excel import HttpExcel suite = unittest.TestSuite() loader = unittest.TestLoader() from class_demo_login_topup import http_tools_1 suite.addTest(loader.loadTestsFromModule(http_tools_1)) # 执行整个文件 with open('http_TestCase.txt', 'w+', encoding='UTF-8') as file: runner = unittest.TextTestRunner(stream=file, verbosity=2) runner.run(suite)
执行ddt方式二
import unittest from class_demo_login_topup.http_tools import Login_Http # 不用ddt的方法 from class_demo_login_topup.http_excel import HttpExcel suite = unittest.TestSuite() loader = unittest.TestLoader() from class_demo_login_topup.http_tools_1 import * # http_tools_1文件是用ddt的方法 suite.addTest(loader.loadTestsFromTestCase(Login_Http)) # 执行http_tools_1 文件下的Login_Http类,按照类执行 with open('http_TestCase.txt', 'w+', encoding='UTF-8') as file: runner = unittest.TextTestRunner(stream=file, verbosity=2) runner.run(suite)
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注猪先飞的更多内容!
相关文章
- 这篇文章主要介绍了python-opencv-画外接矩形框的实例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-04
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 2022虎年新年即将来临,小编为大家带来了一个利用Python编写的虎年烟花特效,堪称全网最绚烂,文中的示例代码简洁易懂,感兴趣的同学可以动手试一试...2022-02-14
- 在本篇文章里小编给大家分享的是一篇关于python中numpy.empty()函数实例讲解内容,对此有兴趣的朋友们可以学习下。...2021-02-06
python-for x in range的用法(注意要点、细节)
这篇文章主要介绍了python-for x in range的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-10- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 这篇文章主要介绍了python如何实现b站直播自动发送弹幕,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-02-20
python Matplotlib基础--如何添加文本和标注
这篇文章主要介绍了python Matplotlib基础--如何添加文本和标注,帮助大家更好的利用Matplotlib绘制图表,感兴趣的朋友可以了解下...2021-01-26- 这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
基于BootStrap Metronic开发框架经验小结【八】框架功能总体界面介绍
这篇文章主要介绍了基于BootStrap Metronic开发框架经验小结【八】框架功能总体界面介绍 的相关资料,需要的朋友可以参考下...2016-05-14- 今天小编就为大家分享一篇python 计算方位角实例(根据两点的坐标计算),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
- 这篇文章主要为大家详细介绍了python实现双色球随机选号,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-05-02
- 在本篇文章里小编给大家整理的是一篇关于python中使用np.delete()的实例方法,对此有兴趣的朋友们可以学习参考下。...2021-02-01
- 这篇文章主要介绍了使用Python的pencolor函数实现渐变色功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-09
- 这篇文章主要介绍了python自动化办公操作PPT的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-05
Python getsizeof()和getsize()区分详解
这篇文章主要介绍了Python getsizeof()和getsize()区分详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-20- 这篇文章主要为大家详细介绍了python实现学生通讯录管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-25
- 这篇文章主要介绍了PyTorch一小时掌握之迁移学习篇,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-08
- 这篇文章主要介绍了解决python 两个时间戳相减出现结果错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-12