接口测试——HTTPRunner二次开发之参数化一
Posted loveapple
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口测试——HTTPRunner二次开发之参数化一相关的知识,希望对你有一定的参考价值。
目前项目中在使用开源框架HTTPRunner,时间使用过程中会用到生成随机信息的方法,如生成随机姓名、随机手机号、身份证号、姓名、地址等。以下对二次开发的过程进行简要描述。
一、需求
1、需要测试的接口:新建患者
2、使用fiddler抓包如下:
这四个接口的功能分别是:
2——获取姓名拼音简写
3——保存患者信息
4——同步数据
5——获取患者处方
3、接口分析
通过查看接口请求参数,需要参数化的信息如下
2中需要传入患者姓名
3中需要传入患者姓名、性别(1,2)、手机号、年龄(1999-09-21)、身份证号、地址
二、设计
分别创建方法,用来随机生成:姓名、性别、手机号、年龄、身份证号和地址
其中
姓名:中文,长度可定制
性别:1男,2女
手机号:
年龄:日期格式xxxx-xx-xx
身份证号:
地址:中文,长度可定制
三、实战
1、导出接口文件,保存为har格式。savePatient.har
2、将导出的har文件转成json格式
har2case savePatient.har savePatient.json
3、在debugtalk.py中增加如下代码
1 import os 2 import random 3 import string 4 import time 5 import datetime 6 7 8 # 常用汉字Unicode编码表 9 StringBase = ‘u7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u6765u4ed6u8fd9u4e0au7740u4e2au5730u5230u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548cu90a3u8981u4e0bu770bu5929u65f6u8fc7u51fau5c0fu4e48u8d77u4f60u90fdu628au597du8fd8u591au6ca1u4e3au53c8u53efu5bb6u5b66u53eau4ee5u4e3bu4f1au6837u5e74u60f3u751fu540cu8001u4e2du5341u4eceu81eau9762u524du5934u9053u5b83u540eu7136u8d70u5f88u50cfu89c1u4e24u7528u5979u56fdu52a8u8fdbu6210u56deu4ec0u8fb9u4f5cu5bf9u5f00u800cu5df1u4e9bu73b0u5c71u6c11u5019u7ecfu53d1u5de5u5411u4e8bu547du7ed9u957fu6c34u51e0u4e49u4e09u58f0u4e8eu9ad8u624bu77e5u7406u773cu5fd7u70b9u5fc3u6218u4e8cu95eeu4f46u8eabu65b9u5b9eu5403u505au53ebu5f53u4f4fu542cu9769u6253u5462u771fu5168u624du56dbu5df2u6240u654cu4e4bu6700u5149u4ea7u60c5u8defu5206u603bu6761u767du8bddu4e1cu5e2du6b21u4eb2u5982u88abu82b1u53e3u653eu513fu5e38u6c14u4e94u7b2cu4f7fu5199u519bu5427u6587u8fd0u518du679cu600eu5b9au8bb8u5febu660eu884cu56e0u522bu98deu5916u6811u7269u6d3bu90e8u95e8u65e0u5f80u8239u671bu65b0u5e26u961fu5148u529bu5b8cu5374u7ad9u4ee3u5458u673au66f4u4e5du60a8u6bcfu98ceu7ea7u8ddfu7b11u554au5b69u4e07u5c11u76f4u610fu591cu6bd4u9636u8fdeu8f66u91cdu4fbfu6597u9a6cu54eau5316u592au6307u53d8u793eu4f3cu58ebu8005u5e72u77f3u6ee1u65e5u51b3u767eu539fu62ffu7fa4u7a76u5404u516du672cu601du89e3u7acbu6cb3u6751u516bu96beu65e9u8bbau5417u6839u5171u8ba9u76f8u7814u4ecau5176u4e66u5750u63a5u5e94u5173u4fe1u89c9u6b65u53cdu5904u8bb0u5c06u5343u627eu4e89u9886u6216u5e08u7ed3u5757u8dd1u8c01u8349u8d8au5b57u52a0u811au7d27u7231u7b49u4e60u9635u6015u6708u9752u534au706bu6cd5u9898u5efau8d76u4f4du5531u6d77u4e03u5973u4efbu4ef6u611fu51c6u5f20u56e2u5c4bu79bbu8272u8138u7247u79d1u5012u775bu5229u4e16u521au4e14u7531u9001u5207u661fu5bfcu665au8868u591fu6574u8ba4u54cdu96eau6d41u672au573au8be5u5e76u5e95u6df1u523bu5e73u4f1fu5fd9u63d0u786eu8fd1u4eaeu8f7bu8bb2u519cu53e4u9ed1u544au754cu62c9u540du5440u571fu6e05u9633u7167u529eu53f2u6539u5386u8f6cu753bu9020u5634u6b64u6cbbu5317u5fc5u670du96e8u7a7fu5185u8bc6u9a8cu4f20u4e1au83dcu722cu7761u5174u5f62u91cfu54b1u89c2u82e6u4f53u4f17u901au51b2u5408u7834u53cbu5ea6u672fu996du516cu65c1u623fu6781u5357u67aau8bfbu6c99u5c81u7ebfu91ceu575au7a7au6536u7b97u81f3u653fu57ceu52b3u843du94b1u7279u56f4u5f1fu80dcu6559u70edu5c55u5305u6b4cu7c7bu6e10u5f3au6570u4e61u547cu6027u97f3u7b54u54e5u9645u65e7u795eu5ea7u7ae0u5e2eu5566u53d7u7cfbu4ee4u8df3u975eu4f55u725bu53d6u5165u5cb8u6562u6389u5ffdu79cdu88c5u9876u6025u6797u505cu606fu53e5u533au8863u822cu62a5u53f6u538bu6162u53d4u80ccu7ec6‘ 10 StringBase = ‘‘.join(StringBase.split(‘\\u‘)) 11 12 13 def gen_random_name(*args): 14 ‘‘‘ 15 author:liuyujie 16 生成随机姓名 17 ‘‘‘ 18 result = ‘‘ 19 # rangestart, rangeend = 2, 5 20 for i in range(random.randint(args[0], args[1])): 21 result += random.choice(StringBase) 22 return result 23 24 25 def gen_random_address(): 26 ‘‘‘ 27 随机生成地址 28 ‘‘‘ 29 address = ‘‘ 30 rangestart, rangeend = 10, 30 # 假设地址在10-30个汉字之间 31 for i in range(random.randint(rangestart, rangeend)): 32 address += random.choice(StringBase) 33 return address 34 35 36 def gen_random_sex(): 37 ‘‘‘ 38 随机生成性别:男1,女2 39 ‘‘‘ 40 return random.randint(1, 2) 41 42 43 def gen_random_date(): 44 ‘‘‘ 45 随机生成1971年到今天的日期 46 ‘‘‘ 47 start_date = (1971, 1, 1, 0, 0, 0, 0, 0, 0) # 设置开始日期时间元组(1976-01-01 00:00:00) 48 start = time.mktime(start_date) # 生成开始时间戳 49 end = int(time.time()) 50 t = random.randint(start, end) # 在开始和结束时间戳中随机取出一个 51 date_touple = time.localtime(t) # 将时间戳生成时间元组 52 date = time.strftime("%Y-%m-%d", date_touple) # 将时间元组转成格式化字符串(1976-05-21) 53 return date 54 55 56 def gen_random_telephone(): 57 ‘‘‘ 58 随机生成手机号 59 ‘‘‘ 60 prelist = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "153", 61 "155", "156", "157", "158", "159", "186", "187", "188"] 62 return random.choice(prelist) + "".join(random.choice("0123456789") for i in range(8)) 63 64 65 def getdistrictcode(): 66 ‘‘‘ 67 获取地区码 68 ‘‘‘ 69 with open(r‘E:xxxHttpRunner-master estsdatadistrictcode.txt‘) as file: # 需要下载districtcode.txt文件 70 data = file.read() 71 districtlist = data.split(‘ ‘) 72 for node in districtlist: 73 # print node 74 if node[10:11] != ‘ ‘: 75 state = node[10:].strip() 76 if node[10:11] == ‘ ‘ and node[12:13] != ‘ ‘: 77 city = node[12:].strip() 78 if node[10:11] == ‘ ‘ and node[12:13] == ‘ ‘: 79 district = node[14:].strip() 80 code = node[0:6] 81 codelist.append({"state": state, "city": city, "district": district, "code": code}) 82 83 84 def gen_random_ID(): 85 ‘‘‘ 86 随机生成身份证号 87 ‘‘‘ 88 global codelist 89 codelist = [] 90 if not codelist: 91 getdistrictcode() 92 id = codelist[random.randint(0, len(codelist))][‘code‘] # 地区项 93 id = id + str(random.randint(1930, 2013)) # 年份项 94 da = datetime.date.today() + datetime.timedelta(days=random.randint(1, 366)) # 月份和日期项 95 id = id + da.strftime(‘%m%d‘) 96 id = id + str(random.randint(100, 300)) # ,顺序号简单处理 97 i = 0 98 count = 0 99 weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 权重项 100 checkcode = {‘0‘: ‘1‘, ‘1‘: ‘0‘, ‘2‘: ‘X‘, ‘3‘: ‘9‘, ‘4‘: ‘8‘, ‘5‘: ‘7‘, ‘6‘: ‘6‘, ‘7‘: ‘5‘, ‘8‘: ‘5‘, ‘9‘: ‘3‘, 101 ‘10‘: ‘2‘} # 校验码映射 102 for i in range(0, len(id)): 103 count = count + int(id[i]) * weight[i] 104 id = id + checkcode[str(count % 11)] # 算出校验码 105 return id
其中districtcode.txt需要下载,地址如下https://pan.baidu.com/s/1lTwFmtoXKrumL1XtkGLFcg
4、修改json测试用例,使用debugtalk.py中的方法进行参数化
代码示例
"test": { "name": "/patient/clinicPatient/savePatientFromPad", "request": { "params": { "detail": "{"body":{"synCode":"","content":{},"param":{"id":"9cc01ecefb784f03bfcc5a87c3ad6a71","userName":"$name","userShortName":"$content","birthday":${gen_random_date()},"sex":${gen_random_sex()},"phone":${gen_random_telephone()},"isDelete":0,"illHistory":"","allergicHistory":"","address":"","homeAddress":${gen_random_address()},"base_version":"","totalArrears":"0","clinicId":"","doctorId":"","idCardNo":${gen_random_ID()},"agency":"","folk":"","validitytime":"","fingerPrint":"0","deviceType":"0","diagnose":"","patientSource":""},"code":{}},"header":{"v":"v_pc_1.0.3.24","doctorMainId":"ff80808162bc9a2f0162f1c2322627ab","clinicId":"8a9a9c745f9b87c3015fdcc1e36b4271","type":"4","imei":"661D-6DF1-B196-3DFE-DE7D-3A01-3AA1-471F","userToken":"20180803105231387f8ueqor"}}" }, "url": "http://xxx/patient/clinicPatient/savePatientFromPad", "method": "GET" }, "extract": [ {"patientId": "content.body.param.id"} ], "validate": [ { "eq": [ "status_code", 200 ] }, { "eq": [ "headers.Content-Type", "text/html;charset=UTF-8" ] } ] } },
5、新建脚本入口文件TestBegin.py
代码如下
1 from httprunner import HttpRunner 2 3 kwargs = { 4 "failfast": False, 5 # "dot_env_path": "/path/to/.env" 6 } 7 runner = HttpRunner(**kwargs) 8 9 runner.run("E:\\xxx\\HttpRunner-master\\tests\\testcases\\savePatient.json") 10 runner.gen_html_report() 11 runner.gen_html_report(html_report_name="reportTest", 12 html_report_template=r"E:xxxHttpRunner-masterhttprunner emplatesdefault_report_template.html")
运行结果如下
/myinfo/pinyin4j/converterToFirstSpell . /patient/clinicPatient/savePatientFromPad /sync/synchronizeData/synchronizeData .. /workbench/prescription/findPrescriptionByPatient . ---------------------------------------------------------------------- Ran 4 tests in 0.285s OK
测试报告如下
在fiddler查看结果
在系统验证结果
以上是关于接口测试——HTTPRunner二次开发之参数化一的主要内容,如果未能解决你的问题,请参考以下文章