整理了一些软件测试方面的资料、面试资料(接口自动化、web自动化、app自动化、性能安全、测试开发等),有需要的小伙伴可以文末加入我的学习交流qun,无套路自行领取~

1.从实际项目出发分析测试结果处理方法

假设我们已经获取了JSON数据

# user_data.json
{"/api3/getbanneradvertver2": {"status": 0,"data": {"banner": [{"id": 2262,"type": 6,"type_id": 330,"name": "\u524d\u7aef\u4e0b\u4e00\u4ee3\u5f00\u53d1\u8bed\u8a00TypeScript  \u4ece\u57fa\u7840\u5230axios\u5b9e\u6218","pic": "http:\/\/szimg.caichenwang.com\/5cf721df09fc2be500000000.jpg","links": ""}, {"id": 1648,"type": 6,"type_id": 169,"name": "Python3\u5165\u95e8\u673a\u5668\u5b66\u4e60 \u7ecf\u5178\u7b97\u6cd5\u4e0e\u5e94\u7528","pic": "http:\/\/szimg.caichenwang.com\/5d0ed2d9085bd6ed09000300.jpg","links": ""}, {"id": 1875,"type": 6,"type_id": 316,"name": "\u4ece\u57fa\u7840\u5230\u5b9e\u6218 \u624b\u628a\u624b\u5e26\u4f60\u638c\u63e1\u65b0\u7248Webpack4.0","pic": "http:\/\/szimg.caichenwang.com\/5d0ed2ca086a9e6f09000300.jpg","links": ""}, {"id": 1999,"type": 6,"type_id": 342,"name": "\u7eaf\u6b63\u5546\u4e1a\u7ea7\u5e94\u7528 Node.js Koa2\u5f00\u53d1\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u670d\u52a1\u7aef","pic": "http:\/\/szimg.caichenwang.com\/5ceb5d370955f30f09000300.jpg","links": ""}, {"id": 2158,"type": 99,"type_id": 0,"name": "Spring Cloud\u5fae\u670d\u52a1\u5f00\u53d1\u5b9e\u8df5","pic": "http:\/\/img2.caichenwang.com\/5d088c4009bbebc009000300.jpg","links": "https:\/\/www.caichen.com\/read\/37"}, {"id": 1709,"type": 6,"type_id": 354,"name": "Node.js\u5f00\u53d1\u4eff\u77e5\u4e4e\u670d\u52a1\u7aef \u6df1\u5165\u7406\u89e3RESTful API","pic": "http:\/\/szimg.caichenwang.com\/5d0ed27508f7d96909000300.jpg","links": ""}],"pic": [{"pic": "http:\/\/www.caichen.com\/static\/img\/andriod\/pic\/actual_day@3x.png","pic_night": "http:\/\/www.caichen.com\/static\/img\/andriod\/pic\/actual_night@3x.png","type": 2}, {"pic": "http:\/\/www.caichen.com\/static\/img\/andriod\/pic\/path_day@3x.png","pic_night": "http:\/\/www.caichen.com\/static\/img\/andriod\/pic\/path_night@3x.png","type": 6}, {"pic": "http:\/\/www.caichen.com\/static\/img\/andriod\/pic\/question_day@3x.png","pic_night": "http:\/\/www.caichen.com\/static\/img\/andriod\/pic\/question_night@3x.png","type": 3}, {"pic": "http:\/\/www.caichen.com\/static\/img\/andriod\/pic\/note_day@3x.png","pic_night": "http:\/\/www.caichen.com\/static\/img\/andriod\/pic\/note_night@3x.png","type": 4}, {"pic": "http:\/\/www.caichen.com\/static\/img\/andriod\/pic\/discover_day@3x.png","pic_night": "http:\/\/www.caichen.com\/static\/img\/andriod\/pic\/discover_night@3x.png","type": 5}]},"errorCode": 1001,"errorDesc": "\u6210\u529f","timestamp": 1561269343507}复制代码
  • 处理JSON结果

    1.有些接口不是单一的请求接口,比如我们商品进行评价,我需要判断这个用户是否完成订单,支付等。

    2.在上边的格式中我们发现格式基本是字段固定

    
    {
    "/api3/.....": {"status": 0, # 状态代表成功失败,根据项目决定"data": {"banner": [{"id": ....,"type": ....,"type_id": .....,"name": "...","pic": "....","links": "..."}.....]},"errorCode": ...,"errorDesc": "....","timestamp": ....
    }复制代码

    3.编写excel测试用例

2.通过接口获取对应得code和message

需求:通过message和errorcode获取匹配的消息,根据url获取结果# code_message.json
# 假设我访问每个端口,返回状态码与匹配信息如下,前期工作量较大
{
"api3/getbanneradvertver2":[{"1006":"token error"},{"1006":"用户名错误"},{"1006":"密码错误"}
],
"api3/beta4":[{"1006":"登陆成功"},{"1006":"用户名错误"},{"1006":"密码错误"}
],
"api3/getcourseintro":[{"1006":"token error"},{"10001":"用户名错误"},{"10002":"密码错误"}
]
}复制代码

3.封装获取message的值

#handle_result.py
import sys
import os
import configparser
base_path = os.getcwd()
sys.path.append(base_path)
import json
from deepdiff import DeepDiff
from Util.handle_json import get_value
#print(get_value('api3/getbanneradvertver2',"/Config/code_message.json"))
'''[{"1006,":"token error"},{"10001":"用户名错误"},{"10002":"密码错误"}]'''def handle_result(url,code):data = get_value(url,"/Config/code_message.json")if data !=None:for i in data:message = i.get(str(code))if message:return messagereturn Nonedef get_result_json(url,status):data = get_value(url,"/Config/result.json")if data !=None:for i in data:message = i.get(status)if message:return messagereturn Nonedef handle_result_json(dict1,dict2):'''校验格式'''if isinstance(dict1,dict) and isinstance(dict2,dict):#dict1={"aaa":"AAA","bbb":"BBBB","CC":[{"11":"22"},{"11":"44"}]}#dict2={"aaa":"123","bbb":"456","CC":[{"11":"111"},{"11":"44"}]}cmp_dict = DeepDiff(dict1,dict2,ignore_order=True).to_dict()if cmp_dict.get("dictionary_item_added"):return Falseelse:return Truereturn Falseif __name__ == "__main__":dict2 = {"aaa":"ddd","aaa1":"A1A","bbb":"BBBB","CC":[{"11":"22"},{"11":"44"}]}dict1={"aaa":"AAA","bbb":"BBBB","aaa3":"A1A","CC":[{"11":"22"},{"11":"44"}]}#print(handle_result('api3/getbanneradvertver2',"10002"))#print(handle_result_json(dict1,dict2))print(get_result_json("api3/newcourseskill","error"))复制代码

4.操作cookie

  • 操作cookie文件设计
#cookie.json
{"app": {"aosid":"LKJALKSJDKAHH1K2JH3J12V3HJ123HASBVABNSD"},"web": {"cookie":"123123123123123123123"}
}复制代码
#handle_json.py
import sys
import os
import configparser
base_path = os.getcwd()
sys.path.append(base_path)
import json
from jsonpath_rw import jsonpath,parsedef read_json(file_name=None):if file_name == None:file_path = base_path+"/Config/user_data.json"else:file_path = base_path+file_namewith open(file_path,encoding='UTF-8') as f:data = json.load(f)return datadef get_value(key,file_name=None):data = read_json(file_name)return data.get(key)#return data[key]def write_value(data,file_name=None):'''{url:{}}'''data_value  = json.dumps(data)if file_name == None:path  = base_path+"/Config/cookie.json"else:path = base_path+file_namewith open(path,"w") as f:f.write(data_value)if __name__ == "__main__":data = {"app":{"aaaa":"bbbbbb"}    }复制代码
  • 更新cookie文件
#coding=utf-8
import sys
import os
import configparser
base_path = os.getcwd()
sys.path.append(base_path)
import json
from Util.handle_json import get_value,read_json,write_valuedef get_cookie_value(cookie_key):'''获取cookie'''data = read_json("/Config/cookie.json")return data[cookie_key]def write_cookie(data,cookie_key):'''写入cookie'''data1 = read_json("/Config/cookie.json")data1[cookie_key] = datawrite_value(data1)if __name__ == "__main__":data = {"aaaa":"1111111111111111"}print(write_cookie(data,'web'))复制代码

5.检查case是否通过

# handle_excel
#coding=utf8
import openpyxl
from openpyxl import workbook
import sys
from collections.abc import Iterable
import os
base_path = os.getcwd()
sys.path.append(base_path)class HandExcel:def load_excel(self):'''加载excel'''open_excel = openpyxl.load_workbook(base_path+"/Case/imooc.xlsx")return open_exceldef get_sheet_data(self,index=None):'''加载所有sheet的内容'''sheet_name = self.load_excel().sheetnamesif index == None:index = 0data = self.load_excel()[sheet_name[index]]return datadef get_cell_value(self,row,cols):'''获取某一个单元格内容'''data = self.get_sheet_data().cell(row=row,column=cols).valuereturn datadef get_rows(self):'''获取行数'''row = self.get_sheet_data().max_rowreturn rowdef get_rows_value(self,row):'''获取某一行的内容'''row_list = []for i in self.get_sheet_data()[row]:row_list.append(i.value)return row_listdef excel_write_data(self,row,cols,value):'''写入数据'''wb = self.load_excel()wr = wb.activewr.cell(row,cols,value)wb.save(base_path+"/Case/imooc.xlsx")def get_columns_value(self,key=None):'''获取某一列得数据'''columns_list = []if key==None:key = 'A'columns_list_data = self.get_sheet_data()[key]for i in columns_list_data:columns_list.append(i.value)return columns_listdef get_rows_number(self,case_id):'''获取行号'''num = 1cols_data = self.get_columns_value()for col_data in cols_data:if case_id == col_data:return numnum = num+1return numdef get_excel_data(self):'''获取excel里面所有的数据'''data_list = []for i in range(self.get_rows()):data_list.append(self.get_rows_value(i+2))return data_listexcel_data = HandExcel()if __name__ == "__main__":handle = HandExcel()##print(handle.get_rows_number('imooc_001'))print(handle.get_excel_data())复制代码
#run_main.py
#coding=utf-8
import sys
import os
base_path = os.getcwd()
sys.path.append(base_path)
from collections.abc import Iterable
from Util.handle_excel import excel_data
import json
from Util.handle_header import get_header
from Util.handle_result import handle_result,handle_result_json,get_result_json
from Util.handle_cookie import write_cookie,get_cookie_value
from Util.codition_data import get_data
from Base.base_request import request
#['imooc_001', '登陆', 'yes', None, 'login', 'post', '{"username":"111111"}', 'yes', 'message', None]
class RunMain:def run_case(self):rows = excel_data.get_rows()for i in range(rows):cookie=Noneget_cookie = Noneheader = Nonedepend_data = Nonedata = excel_data.get_rows_value(i+2)is_run = data[2]if is_run == 'yes':is_depend = data[3]data1 = json.loads(data[7])if is_depend:'''获取依赖数据'''depend_key = data[4]depend_data = get_data(is_depend)#print(depend_data)data1[depend_key] = depend_datamethod = data[6]url = data[5]is_header = data[9]excepect_method = data[10]excepect_result = data[11]cookie_method = data[8]if cookie_method == 'yes':cookie = get_cookie_value('app')if cookie_method == 'write':'''必须是获取到cookie'''get_cookie={"is_cookie":"app"}if is_header == 'yes':header = get_header()res = request.run_main(method,url,data1,cookie,get_cookie,header)#print(res)code = str(res['errorCode'])message = res['errorDesc']if excepect_method == 'mec': config_message = handle_result(url,code)if message == config_message:excel_data.excel_write_data(i+2,13,"通过")else:excel_data.excel_write_data(i+2,13,"失败")excel_data.excel_write_data(i+2,14,json.dumps(res))if excepect_method == 'errorcode':if excepect_result == code:excel_data.excel_write_data(i+2,14,"通过")else:excel_data.excel_write_data(i+2,13,"失败")excel_data.excel_write_data(i+2,14,json.dumps(res))if excepect_method == 'json':if code == 1000:status_str='sucess'else:status_str='error'excepect_result = get_result_json(url,status_str)result = handle_result_json(res,excepect_result)if result:excel_data.excel_write_data(i+2,13,"通过")else:excel_data.excel_write_data(i+2,13,"失败")excel_data.excel_write_data(i+2,14,json.dumps(res))   if __name__ == "__main__":run = RunMain()run.run_case()复制代码

6.发起请求更新cookie代码

#base_request.py
import sys
import os
import configparser
base_path = os.getcwd()
sys.path.append(base_path)
import requests
import json
from Util.handle_cookie import write_cookie
from Util.handle_json import get_value
from Util.handle_init import handle_iniclass BaseRequest:def send_post(self,url,data,cookie=None,get_cookie=None,header=None):'''发送post请求'''response = requests.post(url=url,data=data,cookies=cookie,headers=header)if get_cookie !=None:'''{"is_cookie":"app"}'''cookie_value_jar =  response.cookiescookie_value = requests.utils.dict_from_cookiejar(cookie_value_jar)write_cookie(cookie_value,get_cookie['is_cookie'])res = response.textreturn resdef send_get(self,url,data,cookie=None,get_cookie=None,header=None):'''发视get请求'''response = requests.get(url=url,params=data,cookies=cookie,headers=header)if get_cookie !=None:cookie_value_jar = response.cookiecookie_value = requests.utils.dict_from_cookiejar(cookie_value_jar)write_cookie(cookie_value,get_cookie['is_cookie'])res = response.textreturn resdef run_main(self,method,url,data,cookie=None,get_cookie=None,header=None):'''执行方法,传递method、url、data参数'''#return get_value(url)base_url = handle_ini.get_value('host')if 'http' not in url:url = base_url+urlif method == 'get':res = self.send_get(url,data,cookie,get_cookie,header)else:res = self.send_post(url,data,cookie,get_cookie,header)try:res = json.loads(res)except:print("这个结果是一个text")print("--->",res)return resrequest = BaseRequest()
if __name__ == "__main__":request = BaseRequest() request.run_main('get','http://www.baidu.com/login',"{'username':'11111'}")复制代码

7.项目依赖数据

项目中通过Fiddler请求数据,查看哪些接口需要依赖数据,在excel中写明

#coding=utf-8
import sys
import os
base_path = os.getcwd()
sys.path.append(base_path)
from Util.handle_excel import excel_data
from jsonpath_rw import parse
import jsondef split_data(data):'''拆分单元格数据'''#imooc_005>data:banner:idcase_id = data.split(">")[0]rule_data = data.split(">")[1]return case_id,rule_datadef depend_data(data):'''获取依赖结果集'''case_id = split_data(data)[0]row_number = excel_data.get_rows_number(case_id)data = excel_data.get_cell_value(row_number,14)return datadef get_depend_data(res_data,key):'''获取依赖字段'''res_data = json.loads(res_data)json_exe = parse(key)madle = json_exe.find(res_data)return [math.value for math in madle][0]def get_data(data):'''获取依赖数据'''res_data = depend_data(data)rule_data = split_data(data)[1]return get_depend_data(res_data,rule_data)if __name__ == "__main__":#print(depend_data("imooc_007>data:banner:id"))data = {"a":"a1","b":"b1","c":[{"d":"d1"},{"d":"d2"}]}key = 'c.[1].d'print(get_depend_data(data,key))
复制代码

8.最终数据验证

9.数据驱动和生成报告

#案例
#coding=utf-8
import sys
import os
base_path = os.getcwd()
sys.path.append(base_path)
import ddt
import unittest
from Util.handle_excel import excel_data
data = excel_data.get_excel_data()
#data = [[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7],[4,5,6,7,8]]@ddt.ddt
class TestCase01(unittest.TestCase):def setUp(self):print("case开始执行")def tearDown(self):print("case执行结束")@ddt.data(*data)def test_01(self,data1):#case编号   作用  是否执行    前置条件    依赖key   url method  data    cookie操作    header操作    预期结果方式  预期结果    result  数据function,is_run,condition,depend_key,url,method,request_data,cookie,header,execpet_method,execpet,result,result_data =data#casename,casenum,isrun,method,cookie = data1print("this is test case",function,is_run,condition,depend_key,url,method,request_data,cookie,header,execpet_method,execpet,result,result_data)if __name__ == "__main__":unittest.main()复制代码
#run_case_ddt.py
#coding=utf-8
import sys
import os
base_path = os.getcwd()
sys.path.append(base_path)
from collections.abc import Iterable
from Util.handle_excel import excel_data
import json
import unittest
#from ddt import ddt,data,file_data,unpack
import ddt
import HTMLTestRunner
from Util.handle_header import get_header
from Util.handle_result import handle_result,handle_result_json,get_result_json
from Util.handle_cookie import write_cookie,get_cookie_value
from Base.base_request import request
#['imooc_001', '登陆', 'yes', None, 'login', 'post', '{"username":"111111"}', 'yes', 'message', None]data = excel_data.get_excel_data()
@ddt.ddt
class TestRunMain(unittest.TestCase):@ddt.data(*data)def testrun_case(self,data):#rows = excel_data.get_rows()#for i in range(rows):cookie=Noneget_cookie = Noneheader = None#data = excel_data.get_rows_value(i+2)is_run = data[2]if is_run == 'yes':method = data[5]url = data[4]data1 = data[6]is_header = data[8]excepect_method = data[9]excepect_result = data[10]codition = data[3]if codition:passcookie_method = data[7]if cookie_method == 'yes':cookie = get_cookie_value('app')if cookie_method == 'write':'''必须是获取到cookie'''get_cookie={"is_cookie":"app"}if is_header == 'yes':header = get_header()res = request.run_main(method,url,data1,cookie,get_cookie,header)#print(res)code = str(res['errorCode'])message = res['errorDesc']if excepect_method == 'mec': config_message = handle_result(url,code)self.assertEqual(message,config_message)'''if message == config_message:excel_data.excel_write_data(i+2,12,"通过")else:excel_data.excel_write_data(i+2,12,"失败")excel_data.excel_write_data(i+2,13,json.dumps(res))'''if excepect_method == 'errorcode':self.assertEqual(excepect_result,code)'''if excepect_result == code:excel_data.excel_write_data(i+2,12,"通过")else:excel_data.excel_write_data(i+2,12,"失败")excel_data.excel_write_data(i+2,13,json.dumps(res))'''if excepect_method == 'json':if code == 1000:status_str='sucess'else:status_str='error'excepect_result = get_result_json(url,status_str)result = handle_result_json(res,excepect_result)self.assertTrue(result)'''if result:excel_data.excel_write_data(i+2,12,"通过")else:excel_data.excel_write_data(i+2,12,"失败")excel_data.excel_write_data(i+2,13,json.dumps(res)) '''  def add_case():case_path = os.path.join(base_path, "Run")discover = unittest.defaultTestLoader.discover(case_path,pattern='test_run_*.py')return discoverif __name__ == "__main__":file_path = base_path+'/Report/report.html'with open(file_path,'wb') as f:runner = HTMLTestRunner.HTMLTestRunner(stream=f,title="this is test",description="Mushishi test")runner.run(add_case())f.close()复制代码

好了各位,以上就是这篇文章的全部内容了,能看到这里人啊,都是人才。

如果这个文章写得还不错,觉得我有点东西的话 求点赞

【30天学会接口自动化测试】接口自动化测试之实际项目做接口测试(6)相关推荐

  1. 技术分享 | 接口自动化测试中如何对xml 格式做断言验证?

    在服务端自动化测试过程中,发起请求之后还需要对响应值进行验证,验证响应信息符合预期值之后,这一条接口自动化测试用例才算完整的通过.所以这一章节,将会讲解在接口自动化测试中,是如何对服务端返回的 XML ...

  2. 接口自动化测试实践指导(中):接口测试场景有哪些

    在第一篇文章 接口自动化测试实践指导(上):接口自动化需要做哪些准备工作中详细给小伙伴们讲解了一下接口自动化需要做哪些准备工作,准备工作中最后一步接口测试用例设计是非常重要的一个环节,用例设计的好不好 ...

  3. 【python+pytest】接口自动化测试—接口基础篇

    目录 前言 接口定义 接口分类 常见接口 测试分层 测试左移和右移 写在最后,给与的建议: 前言 就当前软件测试招聘的市场需求以及趋势而言,接口测试是测试人员必须掌握的技能,而接口自动化测试则是加薪利 ...

  4. jmeter接口测试-接口文档信息不完善怎么做接口测试(一)

    做接口测试,首先要有完善的接口文档,我们项目组开发的接口文档适合开发阅读和使用,测试看的话就比较粗糙了,下面看下开发的接口文档: 仅从这上面看的的话很多信息都不清楚,服务器地址端口.接口请求路径.接口 ...

  5. 年薪20W的自动化测试工程师教你用Postman做接口测试

    The higher your test coverage, the more flexible and bug-resistant your code will be, and the less t ...

  6. 接口自动化测试-接口封装思想

    目录 一.接口测试封装思想 二.测试框架 三.架构管理 一.接口测试封装思想 配置--根据配置文件获取初始配置和依赖 接口封装--1.封装接口调动进行抽象封装 --2.类似PageObject效果 业 ...

  7. pytest接口自动化测试框架 | 汇总

    视频来源:B站<冒死上传!pytest接口自动化测试框架(基础理论到项目实战及二次开发)教学视频[软件测试]> 一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持! ...

  8. 【1个月快速学习自动化测试】接口自动化测试(4) —— 接口自动化测试工具介绍

    接口测试的全称是应用程序编程接口(API)测试,从原理上来说,接口测试是模拟客户端向服务器端发送请求,然后检查能否获得正确的返回信息.接口测试用于测试RESTful API.SOAP Web服务,这些 ...

  9. 接口自动化测试实践指导(下):接口自动化测试断言设置思路

    在前篇文章: 接口自动化测试实践指导(中):接口测试场景有哪些 中详细给小伙伴们讲解了一下接口自动化需要做哪些准备工作及接口测试场景有哪些. 本篇文章是最后一篇,主要分享一下接口自动化测试断言设置思路 ...

最新文章

  1. 继续着茫茫碌碌的日子
  2. c语言存储多个数据,C语言中如何求数组真实存有数据的元素个数
  3. 企业内网中的WSUS更新服务 服务器连接到Microsoft Update来获取更新程序
  4. 四川大学锦江学院计算机专业怎么样,四川大学锦江学院怎么样?是一所什么层次的大学?...
  5. vue jsx 使用 自定义组件
  6. 【AI视野·今日CV 计算机视觉论文速览 第197期】Thu, 13 May 2021
  7. [NFrog]工具(NHibernate)终于成形了
  8. OpenNLP-引言
  9. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_24-页面预览-页面预览测试
  10. 机器视觉及视觉传感器
  11. 如何解决录音嗡嗡嗡?VoiceMeeter加OBS免费且强大的录音录像解决方案
  12. win11 windows 服务打开word 另存为pdf
  13. 什么是变量?变量的本质是什么?变量的生命周期和作用域
  14. ios开发常用英文单词总结
  15. 解决spring源码构建时缺失spring-cglib-repack和spring-objenesis-repack问题
  16. 解决Chrome 内置的翻译功能翻译用不了
  17. 互联网时代,你我皆楚门
  18. 百度地图之云图(热图)预警
  19. android alarmmanager进程,即使Android应用程序关闭,如何确保AlarmManager触发?
  20. 口袋进化服务器维护,口袋进化平民攻略

热门文章

  1. 让城市管理更有效率、有温度,罗湖加速推进全区综合执法队伍正规化建设
  2. pd虚拟机共享网络给mac
  3. DNSPod十问徐樱丹:如何破解To B市场营销的增长困局?
  4. Kubernetes如何使用ReplicationController、Replica Set、Deployment管理Pod
  5. 决战2010-训练自己,战胜伤病和疲惫
  6. CTF—基于BinWalk实现文件提取
  7. mysql8 关闭密码策略_mysql8 参考手册--密码安全策略
  8. 联通先锋卡流量免费升级,天上掉馅饼了?
  9. 老男孩教育50期学员左婷婷-day06-linux命令-下部
  10. CAD制图初学入门之CAD中怎么画牛?