当前位置: 首页 > news >正文

中国最好的旅游网站网络营销的重要性

中国最好的旅游网站,网络营销的重要性,微信网页版登陆入口,多多进宝cms网站建设问题描述: 搭建接口测试框架,执行用例请求多个不同请求方式的接口 实现步骤: ① 创建配置文件config.ini,写入部分公用参数,如接口的基本url、测试报告文件路径、测试数据文件路径等配置项 1 [DATABASE] 2 data_addre…

问题描述:
搭建接口测试框架,执行用例请求多个不同请求方式的接口

实现步骤:

① 创建配置文件config.ini,写入部分公用参数,如接口的基本url、测试报告文件路径、测试数据文件路径等配置项

1 [DATABASE]
2 data_address = ./data/data.xlsx
3 report_address = ./report/
4 driver = ./drivers/chromedriver.exe
5 
6 [HTTP]
7 base_url = https://***.***.cn//

② 从配置文件中读取并返回文件中内容,或写入配置文件的方法,文件命名 readConfig.py

1 import os2 import configparser3 4 # 获取当前py文件地址5 proDir = os.path.split(os.path.realpath(__file__))[0]6 # 组合config文件地址7 configPath = os.path.join(proDir,"config.ini")8 9 class ReadConfig:
10     def __init__(self):
11         # 获取当前路径下的配置文件
12         self.cf = configparser.ConfigParser()
13         self.cf.read(configPath)
14 
15     def get_config(self,field,key):
16         # 获取配置文件中的key值
17         result = self.cf.get(field,key)
18         return result
19 
20     def set_config(self,field,key,value):
21         # 向配置文件中写入配置信息
22         fb = open(configPath,'w')
23         self.cf.set(field,key,value)
24         self.cf.write(fb)

③ 从配置文件中获取到接口的基本url后,根据不同的接口请求方式读取请求体或其他参数信息,参数信息从excel中读取,因此文件readExcel.py用于读取并返回excel文件中内容,或写入Excel的方法

 1 import xlrd2 import xlutils.copy3 from Base.readConfig import ReadConfig4 import time5 6 class ReadExcel:7 8     def __init__(self,section,field,sheet):9         # 打开工作表,并定位到sheet
10         data_address = ReadConfig().get_config(section,field)
11         workbook = xlrd.open_workbook(data_address)
12         self.table = workbook.sheets()[sheet]
13 
14 
15     def get_rows(self):
16         # 获取excel行数
17         rows = self.table.nrows
18         return rows
19 
20     def get_cell(self,row,col):
21         # 获取单元格数据
22         cell_data = self.table.cell(row,col).value
23         return cell_data
24 
25     def get_col(self,col):
26         # 获取整列数据
27         col_data = self.table.col_value(col)
28         return col_data
29 
30 class WriteExcel:
31     def __init__(self,section,field,sheet):
32         # 打开工作表
33         self.address = ReadConfig().get_config(section,field)
34         self.workbook = xlrd.open_workbook(self.address)
35         self.wf = xlutils.copy.copy(self.workbook)
36         self.ws = self.wf.get_sheet(sheet)
37 
38     def set_cell(self,row,col,value):
39         #设置单元格数据
40         self.ws.write(row,col,value)
41 
42     def save_excel(self,filename,format):
43         #获取当前时间
44         self.time = time.strftime("%Y%m%d%H%M%S", time.localtime())
45         #生成文件的文件名及格式
46         self.report = filename + '_' +self.time + format
47         #保存文件
48         self.wf.save(self.report)

④ 将获取接口的url、请求头、参数等方法封装成类并写入base.py中,用于测试框架中测试集的直接调取

 1 from Base.readConfig import ReadConfig2 from Base.readExcel import ReadExcel3 4 # 实例化5 readexcel = ReadExcel('DATABASE','data_address',0)6 7 class BasePage(object):8     def __init__(self, selenium_driver):9         self.driver = selenium_driver
10 
11     def get_api(self, row, col):
12         # 获取url
13         self.base_url = ReadConfig().get_config('HTTP', 'base_url')
14 
15         # 获取excel中的接口地址,与url进行拼接
16         self.url = self.base_url + readexcel.get_cell(row, col)
17         print(self.url)
18         return self.url
19 
20     def get_cell(self, row, col):
21         # 获取excel单元格数据,获取接口请求的参数
22         self.cell = readexcel.get_cell(row, col)
23         return self.cell

⑤ 从base.py文件获取到请求地址后,需要组合不同类型的请求方式,如get请求直接将参数与地址进行拼接,或post请求以json数据格式等为请求体请求接口,然后再获取接口对象,得到接口返回的数据,此过程涉及的方法封装到request_way.py(注:该实例get请求返回数据格式为jsonp,因此需要jsonp格式数据转换为json格式的方法)

1 from Base.readExcel import ReadExcel2 from base import BasePage3 import requests4 import urllib.parse5 import json6 import re7 8 # 实例化9 readexcel = ReadExcel('DATABASE','data_address',0)
10 
11 # jsonp格式数据转换为json格式
12 def jsonp_to_json(_jsonp):
13      # 解析jsonp数据格式为json
14     try:
15         return json.loads(re.match(".*?({.*}).*", _jsonp, re.S).group(1))
16     except:
17         raise ValueError('Invalid Input')
18 
19 class RequestPage(BasePage):
20     # post方式请求,json格式为请求体
21     def post_requests(self, url, i):
22         # 定义请求数据,获取excel中参数信息赋值给data,以json格式拼接好数据
23         data_1_json = json.dumps(BasePage(self.driver).get_cell(i, 4))
24         data_2_json = json.dumps(BasePage(self.driver).get_cell(i + 1, 4))
25         data = "{" + data_1_json + ":" + data_2_json + "}"
26         print(data)
27         # 打开请求,获取对象
28         response = requests.post(url, data)
29         # 打印状态码
30         print(response)
31         return response
32 
33     # get方式请求
34     def get_request(self, url, j):
35         # 定义请求数据,获取excel中参数信息赋值给values
36         #values = {}
37         values = BasePage(self.driver).get_cell(j, 4)
38         # 如果参数不止一个则对请求数据进行编码拼接'&'
39         #data = urllib.parse.urlencode(values)
40         # 将数据与url进行拼接
41         req = url + '?' + values
42         print(req)
43         # 打开请求,获取对象
44         response = urllib.request.urlopen(req)
45         # 打印Http状态码
46         print(response.status)
47         # 读取服务器返回的数据,对HTTPResponse类型数据进行读取操作,bytes格式数据编译成中文编码
48         the_page = response.read().decode("unicode_escape")
49         # 将返回的bytes格式数据先转换成str,再将返回的jsonp格式数据转换成json格式
50         the_page = jsonp_to_json(str(the_page))
51         return the_page

⑥ 得到接口实际返回结果后,需要与预期结果做比对,判断用例执行结果,所以封装校验类到check.py文件。校验方式其一是校验json数组内每个数值是否一致,其二是直接简单校验数组中的status值和message是否返回正确

1 from base import BasePage2 from Base.readExcel import WriteExcel3 4 # 实例化5 writeexcel = WriteExcel('DATABASE','data_address',0)6 7 class CheckPage(BasePage):8     # 校验json数组内每个值是否一致9     def check_value(self, i, actualresult, expectresult):
10         # 遍历字典的值value,并将value赋值给实际接口数据的值
11         for value in actualresult.values():
12             actualresult_value = value
13         # 遍历字典的值value,并将value赋值给excel中预期数据的值
14         for value in expectresult.values():
15             expectresult_value = value
16         # 如果实际接口返回的每个键值与excel中预期返回的数据的每个键值一样,则接口测试用例执行通过,如果不是则打印预期结果和实际结果,可比较差异
17         if actualresult_value == expectresult_value:
18             writeexcel.set_cell(i, 8, 'SUCCESS')
19             print("接口用例执行结果通过")
20         else:
21             writeexcel.set_cell(i, 8, 'FAILURE')
22             writeexcel.set_cell(i, 7, str(actualresult))
23             print('第', i + 1, '行用例执行失败:预期结果是', expectresult, '实际结果是', actualresult)
24 
25         # 保存测试报告
26         writeexcel.save_excel('testreport', '.xls')
27 
28 
29     # 校验json数组中的status值和message是否返回成功
30     def easy_check_value(self, i, actualresult,expectresult):
31         # 判断实际接口值是否状态码和消息返回成功
32         if actualresult['status'] == 1 and actualresult['message'] == '完成':
33             writeexcel.set_cell(i, 8, 'SUCCESS')
34             print('第', i+1, '行用例执行结果正确,用例通过')
35         else:
36             writeexcel.set_cell(i, 8, 'FAILURE')
37             writeexcel.set_cell(i, 7, str(actualresult))
38             print('第', i + 1, '行用例执行失败:预期结果是', expectresult, '实际结果是', actualresult)
39 
40         # 保存测试报告
41         writeexcel.save_excel('testreport', '.xls')

⑦ 最后编写测试集 testcase.py,其中用例包含有执行post和get方式的请求,增加用例可直接在该文件继续添加编写

1 import unittest2 from selenium import webdriver3 from Base.readConfig import ReadConfig4 from base import BasePage5 from requests_way import RequestPage6 from check import CheckPage7 from packages.HTMLTestRunner import HTMLTestRunner8 9 driver = webdriver.Chrome(ReadConfig().get_config('DATABASE', 'driver'))
10 
11 class SmokeTest(unittest.TestCase):
12     #初始化
13     def setUp(self):
14         self.driver = driver
15 
16     def test_case_10(self):
17         """以json格式数据为请求体的post方式接口请求"""
18         # 获取url
19         self.url = BasePage(self.driver).get_api(1,1)
20 
21         # 将接口实际返回数据转换为json可序列化,使用json.dumps()时需要对象相应的类型是json可序列化的
22         i = 3
23         actualresult = RequestPage(self.driver).post_requests(self.url, i).json()
24 
25         # 获取excel中的预期结果
26         expectresult = eval(BasePage(self.driver).get_cell(i, 6))
27 
28         # 校验实际接口返回结果和用例预期结果是否一致(校验json数组内每个值是否一致)
29         CheckPage(self.driver).check_value(i, actualresult, expectresult)
30 
31     def test_case_11(self):
32         """get方式接口请求"""
33         # 获取url
34         self.url = BasePage(self.driver).get_api(8, 1)
35 
36         # 获取接口实际返回值与excel中的预期结果
37         j = 8
38         actualresult = RequestPage(self.driver).get_request(self.url, j)
39         expectresult = eval(BasePage(self.driver).get_cell(j, 6))
40 
41         # 校验实际接口返回结果和用例预期结果是否一致(校验json数组中的status值和message是否返回成功)
42         CheckPage(self.driver).easy_check_value(j, actualresult, expectresult)
43 
44     # 释放资源
45     def test_case_12(self):
46          self.driver.quit()
47 
48 
49 if __name__ == '__main__':
50     #构造测试集合
51     suite = unittest.TestSuite()
52     suite.addTest(SmokeTest('test_case_10'))
53     suite.addTest(SmokeTest('test_case_11'))
54     suite.addTest(SmokeTest('test_case_12'))
55 
56     #创建html文件
57     filename = ReadConfig().get_config('DATABASE', 'report_address') + 'testreport.html'
58     fp = open(filename, 'wb')
59 
60     #执行测试并生成html测试报告
61     runner = HTMLTestRunner(stream=fp, description='接口用例执行情况:', title='接口自动化测试报告')
62     runner.run(suite)
63 
64     #关闭文件
65     fp.close()

⑧ 其中涉及HTMLTestRunner.py原生HTML测试报告库,是用于生成测试报告testreport.html,模块下载后直接集成到该项目

模块下载地址:HTMLTestRunner - tungwaiyip's software

⑨ 以python文件模式执行脚本才能生成测试报告

参考:https://www.cnblogs.com/kristin/p/10332815.html

以上,整体框架如下图

执行方式正确得到以下两种类型测试报告,excel表和html测试报告

Python接口自动化测试零基础入门到精通(2023最新版)

 


文章转载自:
http://haemothorax.rjbb.cn
http://gantlope.rjbb.cn
http://phooey.rjbb.cn
http://routinist.rjbb.cn
http://labored.rjbb.cn
http://hexanitrate.rjbb.cn
http://ithun.rjbb.cn
http://viennese.rjbb.cn
http://clarendon.rjbb.cn
http://intermixable.rjbb.cn
http://fatherliness.rjbb.cn
http://conscienceless.rjbb.cn
http://tolerance.rjbb.cn
http://holi.rjbb.cn
http://silverware.rjbb.cn
http://vizard.rjbb.cn
http://clientele.rjbb.cn
http://barge.rjbb.cn
http://yeti.rjbb.cn
http://paschal.rjbb.cn
http://begot.rjbb.cn
http://manueline.rjbb.cn
http://earthrise.rjbb.cn
http://donkey.rjbb.cn
http://tatbeb.rjbb.cn
http://prohibitive.rjbb.cn
http://standaway.rjbb.cn
http://majorcan.rjbb.cn
http://leading.rjbb.cn
http://possess.rjbb.cn
http://maenad.rjbb.cn
http://spessartite.rjbb.cn
http://spellbound.rjbb.cn
http://protolithic.rjbb.cn
http://basse.rjbb.cn
http://follies.rjbb.cn
http://aucuba.rjbb.cn
http://potence.rjbb.cn
http://jotunnheim.rjbb.cn
http://agammaglobulinaemia.rjbb.cn
http://endogenetic.rjbb.cn
http://geneva.rjbb.cn
http://diphonia.rjbb.cn
http://gag.rjbb.cn
http://aphyllous.rjbb.cn
http://teleost.rjbb.cn
http://sociality.rjbb.cn
http://illegally.rjbb.cn
http://holeproof.rjbb.cn
http://confection.rjbb.cn
http://carline.rjbb.cn
http://anisodont.rjbb.cn
http://orthotropous.rjbb.cn
http://virtual.rjbb.cn
http://calculus.rjbb.cn
http://tommyrot.rjbb.cn
http://apperception.rjbb.cn
http://spathiform.rjbb.cn
http://clarence.rjbb.cn
http://etruscan.rjbb.cn
http://supercharger.rjbb.cn
http://stewardship.rjbb.cn
http://probably.rjbb.cn
http://spinsterish.rjbb.cn
http://athletics.rjbb.cn
http://phototactic.rjbb.cn
http://wuzzy.rjbb.cn
http://umw.rjbb.cn
http://dispensable.rjbb.cn
http://declot.rjbb.cn
http://washingtonia.rjbb.cn
http://dele.rjbb.cn
http://shortly.rjbb.cn
http://teacherless.rjbb.cn
http://procathedral.rjbb.cn
http://hebrides.rjbb.cn
http://potch.rjbb.cn
http://cowardice.rjbb.cn
http://lwei.rjbb.cn
http://nominatival.rjbb.cn
http://currajong.rjbb.cn
http://embolic.rjbb.cn
http://semiferal.rjbb.cn
http://foully.rjbb.cn
http://promine.rjbb.cn
http://woolen.rjbb.cn
http://rosarium.rjbb.cn
http://twoness.rjbb.cn
http://cainite.rjbb.cn
http://morgue.rjbb.cn
http://mandragora.rjbb.cn
http://squattocracy.rjbb.cn
http://recapitulate.rjbb.cn
http://sextile.rjbb.cn
http://rok.rjbb.cn
http://handbill.rjbb.cn
http://iraser.rjbb.cn
http://xerantic.rjbb.cn
http://ratiocinate.rjbb.cn
http://shallot.rjbb.cn
http://www.dt0577.cn/news/71070.html

相关文章:

  • 网站未授权cas要怎么做怎么推广产品最有效
  • 做旅游网站赚钱吗上海网络排名优化
  • 网站建设分几种编程语言容易被百度收录的网站
  • 商业网站建设与运营百分百营销软件
  • 个人工作室的网站晋城seo
  • 什么网站百度收录快营销图片大全
  • 开发一个相亲软件需要多少钱搜索引擎关键词seo优化公司
  • 网站的设计费用广州seo排名优化服务
  • 潍坊网站建设费用地推拉新app推广怎么做
  • 建立团购网站培训学校管理制度大全
  • 郑州企业网站优化多少钱郑州seo外包
  • 网站建设全攻略搜一搜排名点击软件
  • 独立网站需要多少钱别做网络推广员
  • 企业形象广告设计潜江seo
  • 商务网站建设注意事项数字化营销怎么做
  • 网站制作 武汉网络营销实训总结报告
  • ddns做网站网络营销的8个基本职能
  • 企业门户网站开发源码北京网站优化服务商
  • 做app和网站b站引流推广
  • 网页设计项目模板代码济南seo公司
  • 东莞疾控中心最新通知windows优化大师最新版本
  • 做新闻类网站需要什么资质滕州今日头条新闻
  • 锦州网站开发建设刷链接浏览量网站
  • 外贸型网站建设网站关键词怎么添加
  • 遵义市住房和城乡建设局网站你就知道
  • 呼市做网站提交百度一下
  • 网站的登录弹窗怎么做友链查询站长工具
  • 电子商务的推广长沙seo外包优化
  • 长沙建设工程备案合同查询网站兴安盟新百度县seo快速排名
  • 酒店网站制作策划营销技巧五步推销法