本篇内容主要讲解“python中的unittest框架应用详解”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“python中的unittest框架应用详解”吧!
需要导入 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中的unittest框架应用详解”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。