接口

我假设有这样一个接口:

  • 方法:post
  • 功能:根据不同的条件参数,如姓名、年龄、性别、受教育程度等来查询筛选患者用户
  • Body:
{"educationTime": "string","jobType": "string","marrige": "string","medicalHistory": 0,"medicationName": "string","orderBy": "string","pageNumber": 0,"pageSize": 100,"patientAge": "string","patientName": "string","patientSex": 0,"sortKey": "string"
}

需求

  • 接口请求实现:请求参数传入map,然后利用遍历map来修改json文件中的值,最终完成请求体body,传入jsonString进行接口请求,实现患者信息的创建;
public Response createPatient(HashMap<String,Object> map){map.put("_file", "/data/assessmentapp/patientManager/createPatient.json");return getResponseFromYaml("/api/brainPFApp/patientManager/createPatient.yaml",map,tokenPattern);}
  • 用例实现:由于进行筛选的信息很多,排列组合起来非常复杂,因此想要设计一个测试用例可以任意输入参数,也就是任意输入患者的姓名、年龄、性别等信息作为参数进行接口自动化的测试工作。

设计模式实现

  • 以建造者模式设计不同参数的处理方法,这里因为我要将参数传入map,所以不同的参数对应了不同的key,value集合传入map,最终以一个buildPatient方法将对象返回:
package com.appApi.assessmentapp.interfance;import lombok.Data;import java.util.HashMap;@Data
public class CreatePatient {private HashMap<String,Object> map;public static class CreatePatientBuilder {HashMap<String,Object> map1 = new HashMap<>();private CreatePatient createPatient;public CreatePatientBuilder(){createPatient = new CreatePatient();}public CreatePatientBuilder buildEducation(String education){map1.put("$.patient.education",education);createPatient.setMap(map1);return this;}public CreatePatientBuilder buildEducationTime(Integer educationTime){map1.put("$.patient.educationTime",educationTime);createPatient.setMap(map1);return this;}public CreatePatientBuilder buildJobType(String jobType){map1.put("$.patient.jobType",jobType);createPatient.setMap(map1);return this;}public CreatePatientBuilder buildMarrige(String marrige){map1.put("$.patient.marrige",marrige);createPatient.setMap(map1);return this;}public CreatePatientBuilder buildMobilePhone(String mobilePhone){map1.put("$.patient.mobilephone",mobilePhone);createPatient.setMap(map1);return this;}public CreatePatientBuilder buildPatientAge(Integer patientAge){map1.put("$.patient.patientAge",patientAge);createPatient.setMap(map1);return this;}public CreatePatientBuilder buildPatientBirthdate(String patientBirthdate){map1.put("$.patient.patientBirthdate",patientBirthdate);createPatient.setMap(map1);return this;}public CreatePatientBuilder buildPatientName(String patientName){map1.put("$.patient.patientName",patientName);createPatient.setMap(map1);return this;}public CreatePatientBuilder buildAddress(String address){map1.put("$.patient.address",address);createPatient.setMap(map1);return this;}public CreatePatientBuilder buildPatientSex(Integer patientSex){map1.put("$.patient.patientSex",patientSex);createPatient.setMap(map1);return this;}public CreatePatient buildPatient(){return createPatient;}}}
  • 说明
    1) 在CreatePatient类中,有一个内部静态类CreatePatientBuilder,我们使用这个类的时候就会加载它的构造方法CreatePatientBuilder实例化一个CreatePatient
    2) 而每一个build方法(比如buildNamebuildAge等)最终都会返回this本身(静态类CreatePatientBuilder),所以我们可以不断利用.buildXXX.buildXXX的形式来进行赋值;
    3) 最终可以以一个buildPatient方法来返回整个对象,由于我需要map的值,所以我这里就可以通过CreatePatient类来最终获取map的值(其实这里在buildPatient直接返回map1也可以)

用例实现

/*** 创建患者必填项*     address    string*     education*  string*     educationTime*  integer($int32)*     jobType*   string*     marrige*    string*     mobilephone*    string*     patientBirthdate*   string*     patientName*    string*     patientSex* integer($int32)*/@Testvoid createPatient(){String patientName = randomValueUtil.getRandomName();String phoneNumber = randomValueUtil.getRandomPhoneNumber();String patientBirthDate = randomValueUtil.getRandomBirthDate();Integer educationTime = randomValueUtil.getNum(0,22);CreatePatient buildPatient = new CreatePatient.CreatePatientBuilder().buildPatientName(patientName).buildMobilePhone(phoneNumber).buildPatientBirthdate(patientBirthDate).buildEducationTime(educationTime).buildAddress("上海市浦东新区张江镇").buildPatient();HashMap<String, Object> map = buildPatient.getMap();System.out.println(map);patientManager.createPatient(map).then().statusCode(200).body("status",equalTo("1")).body(matchesJsonSchemaInClasspath("responseSchema/assessmentapp/patientManager/createPatient.schema"));
  • 运行打印map结果:
{$.patient.mobilephone=130****2175, $.patient.patientName=向X艳, $.patient.address=上海市浦东新区张江镇, $.patient.patientBirthdate=1919-12-10, $.patient.educationTime=8}
这样就可以完成任意的传参,在接口自动化中使测试用例的灵活拓展性更强,可覆盖程度也可更高。

接口自动化-如何对多参数接口进行任意参数个数传参-基于Java建造者设计模式相关推荐

  1. requests payload_python+Requests接口自动化测试之传递 URL 参数

    Requests传递 URL 参数: 你也许经常想为 URL 的查询字符串(query string)传递某种数据.如果你是手工构建 URL,那么数据会以键/值对的形式置于 URL 中,跟在一个问号的 ...

  2. python调用接口时传多个参数_python接口自动化11-post传data参数案例

    前言: 前面登录博客园的是传json参数,有些登录不是传json的,如jenkins的登录,本篇以jenkins登录为案例,传data参数. 一.登录jenkins抓包 1.登录jenkins,输入账 ...

  3. php url传递参数_python+Requests接口自动化测试之传递 URL 参数

    Requests传递 URL 参数: 你也许经常想为 URL 的查询字符串(query string)传递某种数据.如果你是手工构建 URL,那么数据会以键/值对的形式置于 URL 中,跟在一个问号的 ...

  4. python接口自动化pdf悠悠_python接口自动化13-data和json参数傻傻分不清【悠悠】

    https://www.cnblogs.com/yoyoketang/p/7231384.html 前言 在发post请求的时候,有时候body部分要传data参数,有时候body部分又要传json参 ...

  5. Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理

    1.封装http接口请求 已经实现了一个简单的接口请求,接下来就要考虑封装这个请求,在后面的用例中,只需要传递参数(URL ,Params,cookie,heade,method 等)每次去调用这个请 ...

  6. 虫师JAVA接口自动化pdf下载,2019虫师自动化 Python接口自动化虫师 robotframework虫师 虫师接口自动化源码下载...

    第一套:Python虫师自动化 [5.1G] ┃ ┣━━Python接口测试 [2.7G] ┃ ┃ ┣━━code [23.9K] ┃ ┃ ┃ ┗━━myweb01.zip [23.9K] ┃ ┃ ┣ ...

  7. 虫师 python_2019虫师自动化 Python接口自动化虫师 robotframework虫师 虫师接口自动化源码下载...

    第一套:Python虫师自动化 [5.1G] ┃ ┣━━Python接口测试 [2.7G] ┃ ┃ ┣━━code [23.9K] ┃ ┃ ┃ ┗━━myweb01.zip [23.9K] ┃ ┃ ┣ ...

  8. 接口自动化测试之使用Fiddler测试接口安全性

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 关于Fiddler工具,早在之前的<Python接口自动化测试框架实战开发(一)>.<自动化接口实战(一)> ...

  9. java接口自动化+博客园_java+接口自动化+eclipse之-----环境搭建

    根据金字塔的比重总结,UI测试占用10%,接口测试占有20%,单元测试占用70%.考虑到之前学过一段时间的单元测试.UI测试,而接口测试未曾接触过,所以最近打算看看接口测试是怎么实现的. 首先,我们先 ...

最新文章

  1. 鸟哥的Linux私房菜(基础篇)- 第十九章、认识与分析登录文件
  2. JS校验金额格式的正则表达式
  3. #region 常量和静态变量静态类readonly
  4. 通过PROC信息调节TCP窗口
  5. 3.2 python实战_线性回归的梯度下降法
  6. 会java的鸭子_鸭子在Java中打字? 好吧,不完全是
  7. 郑州智慧岛大数据管委会_数据科学融通应用数学 ‖ 智慧岛大讲堂
  8. c语言中的运算符按位或,|按位或运算符
  9. php header函数的详解,php header函数的详解_PHP教程
  10. (转)检测不到兼容的键盘驱动程序
  11. iPhone公司为了节约成本,都干过什么事情?
  12. NetBIOS和NBNS
  13. java发包工具_小米范工具系列之四:小米范HTTP批量发包器
  14. 如何优雅的停止一个线程?
  15. java服务监控并发送邮件_详解Spring Boot Admin监控服务上下线邮件通知
  16. 算法工程师的一万小时定律
  17. Some weights of the model checkpoint at bert_pretrain were not used when initializing BertModel
  18. 2811: [Apio2012]Guard
  19. Drone 自定义 UI
  20. 外卖返利系统外卖返利公众号外卖返利源码

热门文章

  1. RDP 批处理,密码需要先加密
  2. php版本的charCodeAt()函数
  3. 考研:研究生考试(十五天学完)之研究生学霸重点知识点总结之考研必知(考研时间/科目/必备物件)、【考研政治】/【考研英语】/【考研数学】经验总结(历年规律分析、技巧总结、经验分享)
  4. 红外循迹小车简单实现
  5. 科目二和科目三找准30厘米位置的点位
  6. 【瑞模网】3dmax渲染——灯光缓存
  7. egret引擎下,微信分包,微信登陆,微信分享例子
  8. CSS字体和文本的属性学习
  9. 杀毒软件清除了硬盘数据怎么恢复
  10. IDEA闪退提示:There is not enough memory to perform the requested operation..【已解决】直接有效