用python语言中的request定制接口请求数据、定制接口返回数据断言、并生成定制结果(附源码)

目录

项目结构:

commons.judgeData

commons.operateExcel

commons.request_util

sendRequest.test_changcheng

main.py

data测试数据存放


话不多说,直接附上代码

项目结构:

(随手编写的,结构有些混乱,希望不要嫌弃哈哈)

commons模块:放一些通用工具类

data模块:放测试数据

sendRequest模块:放各类请求

main.py函数:放具体运行代码

commons.judgeData

该代码用来做断言,判断keys是否在value中,并针对不同结果返回标志数据。

传入keys:需要判断的内容;value:请求返回数据内容。

#   coding  : utf-8
# @Author    : 琳达kk

def dipdata1(keys,value):
    if keys['rule_state']==0:
        return "状态为0,跳过"
    else:
        if keys['rule_result_type'] == 0 :
            if keys['rule_result'] in value:
                return "错误"
            else:
                return "正确"
        elif keys['rule_result_type'] == 1:
            if keys['rule_result'] in value:
                return "正确"
            else:
                return "错误"
        else:
            return "未测"

commons.operateExcel

用来操作excel文件,写入

#   coding  : utf-8
# @Author    : 琳达kkk
import openpyxl
import pandas as pd
import numpy as np

# 写入Excel   用openpyxl
# path:写入的文件位置
# sheetName:写入的工作表
# value:一维数组(写入的格式)
def toExcel(path,sheetName,value):

    # 打开工作簿
    workbook=openpyxl.load_workbook(path)
#    # 获得工作表
    sheet=workbook[sheetName]
    maxRow = sheet.max_row


    for i in range(1,len(value)+1):
       # 遍历一维列表传入值
        sheet.cell(maxRow+1,i).value=value[i-1]

#    # 保存
    workbook.save(path)


# 读取并处理excel中的测试数据  用pandas
# path:读取的文件路径
# sheetName:读取的工作表名称
# 读取的表中值规律:第一行是所有字段的名称,其他行是对应名称的测试数据
# 返回值为字典类型:字典名 对应 所有字段的名称,字典值 对应 指定字段的测试数据(列表格式)
def readExcel(path,sheetName):
    # 字典 与 测试数据中第一行数据一致(即字段名)
    keys=["no","title","rule_judgment","sign_num","k1","k2","k3","rule_result_value","rule_state","rule_result_type","rule_result","rule_explain","fextend1","fextend2","ffbnew","ftimes","fprn","fname","fsex","fsexbh","fmonthage","frytz","fcstz","fbirthplace","fnative","fnationality","fidcard","fjob","fjobbh","fstatus","fcurraddr","fcurrtele","fcurrpost","fhkaddr","fdwname","fdwaddr","flxname","frelate","flxaddr","frytj","frytime","frydept","frybs","fcytime","fcydept","fcybs","fdays","fmzzd","fmzzdbh","fjbname","ficdm","otherfjbname","otherficdm","fryinfo","fphzdnum","fifgmyw","fgmyw","fbody","fblood","frh","fkzr","fzrdoctor","fzzdoct","fzydoct","fnurse","fbmy","fjxdoct","fsxdoct","fquality","fzkdoct","fzknurse","fzkrq","fdwtele","fdwpost","flyfs","fhkpost","fisagainry","fop","fopcode","otherfop","otherfopcode","fbirthday","fage","fcountry","fcountrybh","flxtele","fzkdept","fphzd","fphzdbh","fryzdbh","fryzd","fdocname","fopdoct1","fopdoct2","fqiekou","fqiekoubh","fmazui","fmazuibh","fmzdoct","fssjb","fssjbbh","fcurrteleint","fcurrpostint","fhkpostint","fdwteleint","fdwpostint","flxteleint","frytjbh","fryinfobh","fifgmywbh","fbloodbh","frhbh","flyfsbh","fisagainrybh","fqualitybh","fsum1","fsum2","fzllfssf","fzllffssf","fzllfssf1","fxyf","ffbbhnew","fidcardlgc1","fidcardlgc2","fagelgc","fidcardlgc3","fstatuslgc1","fstatuslgc2","frydatelgc1","frydatelgc2","fdayslgc","damageCodelgc","ficdm1","ficdm2","damageCode","otherficdm1","fphzdbh1","fphzdbh2","fphzdbh3","fphzdbh4","fphzdbh5","fopcodelgc","ficdm3","ficdm4","ficdm5","ficdm6","ficdm7","diagnoselgc1","diagnoselgc2","otherDiagnoseCodelgc1","otherDiagnoseCodelgc2","opcodelgc1","opcodelgc2","fcstz2","fcstz3","fcstz4","fcstz5","hbsag","hcvab","hivab","fkzrbh","fzrdoctorbh","fzzdoctbh","fzydoctbh","fnursebh","fopdate","bloodTransfusionReaction","erythrocytes","platelets","plasma","wholeBlood","autologousBloodTransfusion","beforeAdmissionComaDays","beforeAdmissionComaHours","beforeAdmissionComaMinutes","afterAdmissionComaDays","afterAdmissionComaHours","afterAdmissionComaMinutes","invasiveVentilatorUsageTime","intensiveCareUnitName","entryTime","exitTime","fyzouthostital","xsenljy","xsecstz","fmjjbm","cardType","fisagainrymd1","fdate","fmzzdbh1","fzkrq1","damageName","fphzdbh6","damageCode1","operationLevel","operationQiekou","operationMazui","anesthesiaLevel","diagnoseByFage1","diagnoseByFage2","diagnoseByFage3","diagnoseByFage4","diagnoseByFage5","diagnoseByFage6","diagnoseByFage7","diagnoseByFage8","diagnoseByFage9","diagnoseByFage10","diagnoseByFage11","diagnoseByFage12","diagnoseByFage13","diagnoseByFage14","diagnoseByFage15","diagnoseByFage16","fzdbhNotAlone1","fzdbhNotAlone2","fzdbhNotAlone3","fzdbhNotAlone4","diagnoselgc3","diagnoselgc4","diagnoselgc5","diagnoselgc6","diagnoselgc7","diagnoselgc8","diagnoselgc9","diagnoselgc10","fage1","fdate1","fryzdbh1","diagnoseByBh1","frelate1","fage2","fphzdbh7","fphzdbh8","fqjtimes","fsum3","intensiveCareUnitName1","operateFqiekou","fidcardlgc4","ffbbhnew1","fqualitybh1","fdate2","fjob1","fstatuslgc3","fascard1","fhkaddr1","fcurraddr1","fdwaddr1","frytjbh1","fzzyljg","fryrcshpf","fcyrcshpf","otherfopdate","fotherssjb","fotherdocname","fopotherdoct1","fopotherdoct2","fotherqiekou","fothermazui","fothermzdoct","fopmainoperate","fphzdbh9","damageCode2","ficdm8","fzljg","fzljgbh","ficdm9","fbirthplace1","fhkaddr2","fnative1","fbirthplace2","fhkaddr3","fnative2","fphzdbh10","frhbh2","fbodybh","fzhfwlylf","fzhfwlhlf","fzyopcode","fzllfmzf","fzyzl","fzyhc","fhcf","fqtf","ficdm10","fryinfo2","fzljg2","fmzzdbh2","fzhfwlylf2","fzhfwlblzlf","fzhfwlblhzf","fzhfwlczf","fzhfwlhlf2","fzhfwlqtf","fzdlblf","fzdlsssf","fzdlyxf","fzdllcf","fzllffssf2","fzllfwlzwlf","fzllfssf2","fzllfmzf2","fzllfsszlf","fkflkff","fzylzf","fzylzdf","fzylzlf","fzylzlf01","fzylzlf02","fzylzlf03","fzylzlf04","fzylzlf05","fzylzlf06","fzylqtf","fzylqtf01","fzylqtf02","fxyf2","fxylgjf","fzchyf","fzcljgzjf","fzcyf","","fxylxf","fxylbqbf","fxylqdbf","fxylyxyzf","fxylxbyzf","fhclcjf","fhclzlf","fhclssf","fqtf2","fmzzyzdbh","fmzzyzd","fzyicdm1","fzyicdm2","fzyjbname1","fzyjbname2","fzyryinfo","fsyzysb","fsyzyzljs","fimplementclinicalpathway","fsyjgzyzj","fdialecticalprotection","ftreatmentcategory","fzfje","damageReason","mainDiagnoseError1","diagnoseError1","diagnoseError2","diagnoseError3","operationError1","operationError2","pathologicalError1","mainDiagnoseWarn1","mainDiagnoseWarn2","mainDiagnoseWarn3","diagnoseWarn1","diagnoseWarn2","diagnoseWarn3","diagnoseWarn4","diagnoseWarn5","diagnoseWarn6","diagnoseWarn7","diagnoseWarn8","diagnoseWarn9","diagnoseWarn10","diagnoseWarn11","otherDiagnoseWarn1","otherDiagnoseWarn2","otherDiagnoseWarn3","mainDiagnoseWarn4","mainDiagnoseWarn5","mainDiagnoseWarn6","mainDiagnoseWarn7","mainDiagnoseWarn8","mainDiagnoseWarn9","otherDiagnoseWarn4","mainDiagnoseWarn10","mainDiagnoseWarn11","mainDiagnoseWarn12","diagnoseWarn12","operationError3","diagnoseWarn13"]
    # 读取Excel数据
    df = pd.read_excel(path,sheet_name=sheetName)
    # 单个sheet
    # df = pd.read_excel('sheet_name.xlsx', sheet_name=2)
    # df = pd.read_excel('sheet_name.xlsx', sheet_name='3月')
    # # 多个sheet, 返回字典
    # df_dict = pd.read_excel('sheet_name.xlsx', sheet_name=[1, '3月'])
    # # 全部sheet, 返回字典
    # df_dict = pd.read_excel('sheet_name.xlsx', sheet_name=None)

    # 将DataFrame对象转换为numpy数组
    numpy_array = df.values

    # 转换为二维数组
    two_dimensional_array = np.array(numpy_array)
    # 行列转置
    two_dimensional_array = np.transpose(two_dimensional_array)

    # 得到字段数
    maxcolum = len(keys)

    # 将数组转成字典格式
    # 字段:内容
    result = {}
    for i in range(0,maxcolum):
        result[keys[i]] = two_dimensional_array[i]

    return result

commons.request_util

用于存放原始请求(给其他发送请求者调用)

#   coding  : utf-8
# @Author    : 琳达kk
import requests

# 统一请求封装
# 可以用于get、post等多种请求
class RequestUtil:

    sess = requests.session()

    def all_send_request(self,method,url,**kwargs):
        res=RequestUtil.sess.request(method,url,**kwargs)
        return res

sendRequest.test_changcheng

请求发送位置(由于开发用的get发送json数据,此处也是用的get请求,即test_index方法)

#   coding  : utf-8
# @Author    : 琳达kk
from commons.request_util import RequestUtil

class TestChangCheng:
    # 访问接口  get请求   此处用到get请求
    # 用的是 json格式
    def test_index(self,datas):
        res = RequestUtil().all_send_request("get", url="http://192.168.2.28:8099/dip/api/checkTestGroupResult",json=datas)
        result = res.text
        return result

    # 登录接口
    # 未用到,做简单描述
    def test_login(self):
        urls = "http://192.168.116.139/login/index.php?m=u&c=login&a=dorun"
        index_cookies = ""
        headers = {
            "Accept": "application/json,text/javascript, /;q=0.01",
            "X-Requested-With": "XMLHttpRequest"
        }
        datas = {
            "username": "",
            "password": "",
            "csrf_token": "",
            "backurl": "http://192.168.116.139/name",
            "invite": ""
        }

        # 表单用 data 传输数据,对应 postman 里面的 x-www-form-urlencoded
        res = RequestUtil().all_send_request(method="post", url=urls, data=datas, headers=headers,cookies=index_cookies)

        # 请求里面提取json数据
        print(res)



main.py

#   coding  : utf-8
# @Author    : 琳达kkk
from commons.judge_data import dipdata1
from commons.operate_excel import toExcel, readExcel
import numpy as np


from sendRequest.test_changcheng import TestChangCheng


class DealData():
    # 结果数据字段所有名称
    names = ["flag","no","title","rule_judgment","sign_num","k1","k2","k3","rule_result_value","rule_state","rule_result_type","rule_result","rule_explain","fextend1","fextend2","ffbnew","ftimes","fprn","fname","fsex","fsexbh","fmonthage","frytz","fcstz","fbirthplace","fnative","fnationality","fidcard","fjob","fjobbh","fstatus","fcurraddr","fcurrtele","fcurrpost","fhkaddr","fdwname","fdwaddr","flxname","frelate","flxaddr","frytj","frytime","frydept","frybs","fcytime","fcydept","fcybs","fdays","fmzzd","fmzzdbh","fjbname","ficdm","otherfjbname","otherficdm","fryinfo","fphzdnum","fifgmyw","fgmyw","fbody","fblood","frh","fkzr","fzrdoctor","fzzdoct","fzydoct","fnurse","fbmy","fjxdoct","fsxdoct","fquality","fzkdoct","fzknurse","fzkrq","fdwtele","fdwpost","flyfs","fhkpost","fisagainry","fop","fopcode","otherfop","otherfopcode","fbirthday","fage","fcountry","fcountrybh","flxtele","fzkdept","fphzd","fphzdbh","fryzdbh","fryzd","fdocname","fopdoct1","fopdoct2","fqiekou","fqiekoubh","fmazui","fmazuibh","fmzdoct","fssjb","fssjbbh","fcurrteleint","fcurrpostint","fhkpostint","fdwteleint","fdwpostint","flxteleint","frytjbh","fryinfobh","fifgmywbh","fbloodbh","frhbh","flyfsbh","fisagainrybh","fqualitybh","fsum1","fsum2","fzllfssf","fzllffssf","fzllfssf1","fxyf","ffbbhnew","fidcardlgc1","fidcardlgc2","fagelgc","fidcardlgc3","fstatuslgc1","fstatuslgc2","frydatelgc1","frydatelgc2","fdayslgc","damageCodelgc","ficdm1","ficdm2","damageCode","otherficdm1","fphzdbh1","fphzdbh2","fphzdbh3","fphzdbh4","fphzdbh5","fopcodelgc","ficdm3","ficdm4","ficdm5","ficdm6","ficdm7","diagnoselgc1","diagnoselgc2","otherDiagnoseCodelgc1","otherDiagnoseCodelgc2","opcodelgc1","opcodelgc2","fcstz2","fcstz3","fcstz4","fcstz5","hbsag","hcvab","hivab","fkzrbh","fzrdoctorbh","fzzdoctbh","fzydoctbh","fnursebh","fopdate","bloodTransfusionReaction","erythrocytes","platelets","plasma","wholeBlood","autologousBloodTransfusion","beforeAdmissionComaDays","beforeAdmissionComaHours","beforeAdmissionComaMinutes","afterAdmissionComaDays","afterAdmissionComaHours","afterAdmissionComaMinutes","invasiveVentilatorUsageTime","intensiveCareUnitName","entryTime","exitTime","fyzouthostital","xsenljy","xsecstz","fmjjbm","cardType","fisagainrymd1","fdate","fmzzdbh1","fzkrq1","damageName","fphzdbh6","damageCode1","operationLevel","operationQiekou","operationMazui","anesthesiaLevel","diagnoseByFage1","diagnoseByFage2","diagnoseByFage3","diagnoseByFage4","diagnoseByFage5","diagnoseByFage6","diagnoseByFage7","diagnoseByFage8","diagnoseByFage9","diagnoseByFage10","diagnoseByFage11","diagnoseByFage12","diagnoseByFage13","diagnoseByFage14","diagnoseByFage15","diagnoseByFage16","fzdbhNotAlone1","fzdbhNotAlone2","fzdbhNotAlone3","fzdbhNotAlone4","diagnoselgc3","diagnoselgc4","diagnoselgc5","diagnoselgc6","diagnoselgc7","diagnoselgc8","diagnoselgc9","diagnoselgc10","fage1","fdate1","fryzdbh1","diagnoseByBh1","frelate1","fage2","fphzdbh7","fphzdbh8","fqjtimes","fsum3","intensiveCareUnitName1","operateFqiekou","fidcardlgc4","ffbbhnew1","fqualitybh1","fdate2","fjob1","fstatuslgc3","fascard1","fhkaddr1","fcurraddr1","fdwaddr1","frytjbh1","fzzyljg","fryrcshpf","fcyrcshpf","otherfopdate","fotherssjb","fotherdocname","fopotherdoct1","fopotherdoct2","fotherqiekou","fothermazui","fothermzdoct","fopmainoperate","fphzdbh9","damageCode2","ficdm8","fzljg","fzljgbh","ficdm9","fbirthplace1","fhkaddr2","fnative1","fbirthplace2","fhkaddr3","fnative2","fphzdbh10","frhbh2","fbodybh","fzhfwlylf","fzhfwlhlf","fzyopcode","fzllfmzf","fzyzl","fzyhc","fhcf","fqtf","ficdm10","fryinfo2","fzljg2","fmzzdbh2","fzhfwlylf2","fzhfwlblzlf","fzhf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值