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

接口自动化-如何对多参数接口进行任意参数个数传参-基于Java建造者设计模式接口 我假设有这样一个接口 方法 post 功能 根据不同的条件参数 如姓名 年龄 性别 受教育程度等来查询筛选患者用户 Body educationTim string jobType

大家好,我是讯享网,很高兴认识大家。

接口

我假设有这样一个接口:


讯享网

  • 方法: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) */ @Test void 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=, $.patient.patientName=向X艳, $.patient.address=上海市浦东新区张江镇, $.patient.patientBirthdate=1919-12-10, $.patient.educationTime=8} 
这样就可以完成任意的传参,在接口自动化中使测试用例的灵活拓展性更强,可覆盖程度也可更高。
小讯
上一篇 2025-02-18 07:46
下一篇 2025-02-18 09:29

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/119090.html