python接口自动化1
组织架构:
包括配置文件,反射、文件路径、Excel操作、测试报告生成
case.config
[MODE]
file_name=case_data.xlsx
mode={"register":'all',"login":'all',"recharge":'all'}
tools文件夹里的东西
do_config.py
1 # -*- conding:utr-8 -*- 2 #@Time :2018/11/17 11:21 3 #@Author:GYP测试 4 #@File :do_config.py 5 6 import configparser 7 class ReadConfig: 8 def read_config(self,file_name,section,option): 9 cf=configparser.ConfigParser() 10 cf.read(file_name,encoding='utf-8') 11 return cf.get(section,option) 12 if __name__ == '__main__': 13 res=ReadConfig().read_config('case.config','MODE','file_name') 14 print(res)
do_excel.py
1 # -*- conding:utr-8 -*- 2 #@Time :2018/11/17 14:05 3 #@Author:GYP测试 4 #@File :do_excel.py 5 from openpyxl import load_workbook 6 from tools.do_config import ReadConfig 7 from tools.project_path import * 8 9 class Do_Excle: 10 def __init__(self): 11 self.file_name = test_data_path 12 self.sheet_names = eval(ReadConfig().read_config(case_config_path, 'MODE', 'mode')) 13 def get_header(self): 14 wb = load_workbook(self.file_name) 15 for sheet_name in self.sheet_names: 16 sheet=wb[sheet_name] 17 header=[] 18 for i in range(1,sheet.max_column+1): 19 header.append(sheet.cell(1,i).value) 20 return header 21 def Read_Excle(self): 22 wb = load_workbook(self.file_name) 23 test_data = [] 24 for sheet_name in self.sheet_names: 25 sheet = wb[sheet_name] 26 header = self.get_header() 27 if self.sheet_names[sheet_name]=='all': 28 for i in range(2,sheet.max_row+1): 29 sub_data={} 30 for j in range(1,sheet.max_column+1): 31 sub_data[header[j-1]]=sheet.cell(i,j).value 32 test_data.append(sub_data) 33 else: 34 for case_id in self.sheet_names[sheet_name]: 35 sub_data={} 36 for j in range(1,sheet.max_column+1): 37 sub_data[header[j-1]]=sheet.cell(case_id+1,j).value 38 test_data.append(sub_data) 39 return test_data 40 @staticmethod 41 def write_excel(fiel_name,sheet_name,i,ActaulResult): 42 wb=load_workbook(fiel_name) 43 sheet=wb[sheet_name] 44 sheet.cell(i,8).value=ActaulResult 45 wb.save(fiel_name) 46 if __name__ == '__main__': 47 res=Do_Excle().Read_Excle() 48 print(res) 49 print(len(res))
get_data.py
# -*- conding:utf-8 -*- #@Time :2018/11/19 11:37 #@Author:GYP测试 #@File :get_data.py class Get_Data: cookie=None
http_requests.py
1 # -*- conding:utr-8 -*- 2 #@Time :2018/11/16 21:50 3 #@Author:GYP测试 4 #@File :http_requests.py 5 6 import requests 7 class Http_Request: 8 def request(self,method,url,data,cookie=None): 9 try: 10 if method == 'post': 11 res=requests.post(url,data,cookies=cookie) 12 else: 13 res=requests.get(url,data,cookies=cookie) 14 except Exception as e: 15 print("非法请求,请检查{0}".format(e)) 16 raise e 17 return res
http_test.py
1 # -*- conding:utr-8 -*- 2 #@Time :2018/11/18 15:00 3 #@Author:GYP测试 4 #@File :http_test.py 5 6 7 import unittest #单元测试框架 8 9 from tools.do_excel import Do_Excle #数据读写 10 from tools.project_path import * #文件路径 11 from ddt import ddt,data #数据处理框架 12 from tools.get_data import Get_Data #反射 13 from tools.http_requests import Http_Request 14 15 test_data=Do_Excle().Read_Excle() #读取数据 16 @ddt 17 class TestHttp(unittest.TestCase): 18 @data(*test_data) 19 def test_api(self,item): 20 res=Http_Request().request(item['method'],item['url'],eval(item['data']),getattr(Get_Data,'cookie')) 21 if res.cookies: #利用反射获取cookie的值 22 setattr(Get_Data,'cookie',res.cookies) 23 try: 24 self.assertEqual(str(item['ExpectedResult']),res.json()['code']) 25 ActaulResult='Pass' 26 except AssertionError as e: 27 print('执行用例失败,请检查%s' %e) 28 ActaulResult = 'Faile' 29 print("获取到的结果是:{0}".format(res.json())) 30 finally: 31 # print(item['case_id']) 32 Do_Excle.write_excel(test_data_path,item['module'],item['case_id']+1,ActaulResult)
project_path.py
# -*- conding:utr-8 -*- #@Time :2018/11/17 13:46 #@Author:GYP测试 #@File :project_path.py import os from tools.do_config import ReadConfig # class Get_Path: # def get_path(self): path=os.path.split(os.path.split(os.path.realpath(__file__))[0])[0] case_config_path=os.path.join(path,'conf','case.config') data_file_name=ReadConfig().read_config(case_config_path,'MODE','file_name') test_data_path=os.path.join(path,'test_data',data_file_name) html_repot_path=os.path.join(path,'test_result','html_report','test_api.html') print(case_config_path) print(html_repot_path)
run.py
1 # -*- conding:utr-8 -*- 2 #@Time :2018/11/16 21:36 3 #@Author:GYP测试 4 #@File :run.py 5 6 import unittest 7 import HTMLTestRunner 8 from tools.project_path import * 9 10 from tools.http_test import TestHttp 11 suite=unittest.TestSuite() 12 13 14 loader=unittest.TestLoader() 15 16 suite.addTest(loader.loadTestsFromTestCase(TestHttp)) 17 18 with open(html_repot_path,'wb') as file: 19 runner=HTMLTestRunner.HTMLTestRunner( 20 stream=file, 21 title='这个是接口自动化的测试报告', 22 description='我来测试哦!', 23 tester='GYP') 24 runner.run(suite)