HttpRequests.py
1 #-*- coding:utf-8 -*- 2 3 import requests 4 class HttpRequests(): 5 def http_requests(self,url,params,http_mothed,cookies=None): 6 if http_mothed=='get': 7 res=requests.get(url,params,cookies=cookies) 8 return res 9 else: 10 res=requests.post(url,params,cookies=cookies) 11 return res
test_HttpRequests.py
1 #-*- coding:utf-8 -*- 2 3 #存放测试用例 4 import unittest 5 from study_181130_request.HttpRequests import HttpRequests 6 7 8 COOKIES=None 9 10 class TestHttpRequests(unittest.TestCase): 11 12 13 def setUp(self): 14 # self.url='http://47.107.168.87:8080/futureloan/mvc/api/member/login' 15 pass 16 17 def __init__(self,url,params,http_method,excepted,methodName):#初始化传参;用初始化函数一定要记得去看看父类里面有没有初始化函数,如果有,需要超继承 18 19 self.params=params#请求的数据 20 self.http_method=http_method#请求方法 21 self.url=url 22 self.excepted=excepted 23 super(TestHttpRequests,self).__init__(methodName)#超继承 24 25 26 def test_api(self):#登录成功 :手机号正确、密码正确;用例里面不可以传参数 27 global COOKIES#声明全局变量 28 res=HttpRequests().http_requests(self.url,self.params,self.http_method,COOKIES) 29 try: 30 self.assertEqual(self.excepted,res.json()['msg']) 31 except AssertionError as e: 32 print('断言结果是:{}'.format(e)) 33 raise e 34 35 if res.cookies: 36 COOKIES=res.cookies#当res.cookies非空时,修改COOKIES的值
test_runner.py 10-15新增
1 #-*- coding:utf-8 -*- 2 3 4 import unittest 5 import HTMLTestRunnerNew 6 from study_181130_request.test_HttpRequests import TestHttpRequests 7 from study_181130_request.read_excel import TestExcel 8 from study_181130_request.read_config import ReadConfig 9 10 #利用配置类,从配置文件case.conf里面读取section:case;option:button的values值 11 button=ReadConfig().read_config('case.conf','case','button') 12 13 #调用task_04_do_excel这个模块里面的TestExcel类里面的get_TestExcel方法 14 #此方法需要 文件名,表单名,配置的值 这几个参数 15 test_data=TestExcel().get_TestExcel('http_requests.xlsx','Sheet1',button) 16 17 18 suit=unittest.TestSuite()##装用例的地方 19 for item in test_data: 20 suit.addTest(TestHttpRequests(item['url'],eval(item['params']),item['http_method'],item['excepted'],'test_api'))#创建实例的方法来添加用 21 22 #执行用例 23 with open('test_api.html','bw+') as file: 24 Runner=HTMLTestRunnerNew.HTMLTestRunner(stream=file,verbosity=2, 25 title='HTTP请求作业-单元测试报告',description='测试一下HTTP请求',tester='huimin' 26 ) 27 Runner.run(suit)
read_excel.py 23-32行新增
1 #-*- coding:utf-8 -*- 2 3 from openpyxl import load_workbook 4 import configparser 5 6 7 class TestExcel(): 8 9 def get_TestExcel(self,file_name,sheet_name,button): 10 workbook = load_workbook(file_name)#打开表格 11 sheet = workbook[sheet_name]#定位表单 12 13 test_data = []#把所有行的数据放到列表中 14 15 for i in range(2,sheet.max_row+1): 16 sub_data = {}#把每行的数据放到字典中 17 for j in range(1,sheet.max_column+1): 18 19 sub_data[sheet.cell(1,j).value] = sheet.cell(i,j).value 20 21 test_data.append(sub_data)#把每一行数据添加到test_data列表里 22 23 final_data=[]#最后的测试数据 24 if button == 'all': 25 final_data=test_data 26 else: 27 for item in test_data: #遍历test_data里面的数据 28 if item['case_id'] in eval(button): 29 #如果表格中case_id在button里面,就把这条用例加到final_data里面 30 final_data.append(item) 31 32 return final_data
read_config.py -新增
1 #-*- coding:utf-8 -*- 2 3 import configparser 4 class ReadConfig(): 5 def read_config(self,file_name,section,option): 6 cf = configparser.ConfigParser()#创建对象 7 cf.read(file_name,encoding ='utf-8')#打开配置文件 8 value=cf.get(section,option) 9 return value
执行结果: