RestAssured多参数优化:将动态Multipart参数整合至通用函数
问题描述
我有多个API测试用例,每个用例的multipart输入参数集合动态变化,示例测试用例如下:
public test1(){ | KEY| VALUE | |key1 | value1| |key2 | value2| |key3 | value3| } public test2(){ | KEY| VALUE | |key1 | value1| |key2 | value2| } public test3(){ | KEY| VALUE | |key1 | value1| |key2 | value2| |key3 | value3| |key4 | value4| |key5 | value5| }
当前的通用函数需要多次调用multiPart方法,无法适配参数的动态变化:
public void common() { Response response = given().urlEncodingEnabled(true).config(RestAssured.config() .encoderConfig(encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT))) .multiPart("key1","value1") .multiPart("key2","value2") .multiPart("key3","value3") .multiPart("key4","value4") .multiPart("key5","value5") .config(RestAssured.config() .contentType(ContentType.JSON) .headers(request.getHeaders()) .post(path); }
期望实现一次传入所有multipart参数的通用函数,无需多次调用multiPart,类似如下逻辑:
public void common() { given().urlEncodingEnabled(true).config(RestAssured.config() .encoderConfig(encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT))) .multiPart("key1","value1";"key2","value2";"key3","value3";"key4","value4";"key5","value5") .config(RestAssured.config() .contentType(ContentType.JSON) .headers(request.getHeaders()) .post(path); }
实现思路与方案
方法1:使用Map传递动态参数
RestAssured支持直接传入Map<String, Object>作为multipart参数,内部会自动遍历Map生成对应的part。修改通用函数接收Map参数即可适配动态参数:
public void common(Map<String, Object> multipartParams) { Response response = given() .urlEncodingEnabled(true) .config(RestAssured.config() .encoderConfig(encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT))) .multiPart(multipartParams) // 直接传入Map批量生成part .config(RestAssured.config() .contentType(ContentType.JSON)) .headers(request.getHeaders()) .post(path); }
调用时每个测试用例只需构造对应参数的Map:
// test1调用示例 Map<String, Object> params1 = new HashMap<>(); params1.put("key1", "value1"); params1.put("key2", "value2"); params1.put("key3", "value3"); common(params1); // test2调用示例 Map<String, Object> params2 = new HashMap<>(); params2.put("key1", "value1"); params2.put("key2", "value2"); common(params2);
方法2:使用MultiPartSpecBuilder构建精细控制的批量Part
如果需要为每个part单独配置属性(比如自定义ContentType),可以用MultiPartSpecBuilder构建多个MultiPartSpecification,再传入集合:
public void common(List<MultiPartSpecification> multipartSpecs) { Response response = given() .urlEncodingEnabled(true) .config(RestAssured.config() .encoderConfig(encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT))) .multiPart(multipartSpecs) // 传入MultiPartSpecification集合 .config(RestAssured.config() .contentType(ContentType.JSON)) .headers(request.getHeaders()) .post(path); }
构造Spec的示例:
// test3的参数构造示例 List<MultiPartSpecification> specs = new ArrayList<>(); specs.add(new MultiPartSpecBuilder("value1").controlName("key1").build()); specs.add(new MultiPartSpecBuilder("value2").controlName("key2").build()); specs.add(new MultiPartSpecBuilder("value3").controlName("key3").build()); specs.add(new MultiPartSpecBuilder("value4").controlName("key4").build()); specs.add(new MultiPartSpecBuilder("value5").controlName("key5").build()); common(specs);
方法3:动态链式添加multiPart
如果不想修改函数参数结构,也可以在通用函数内部遍历参数动态添加part:
public void common(Map<String, Object> multipartParams) { RequestSpecification requestSpec = given() .urlEncodingEnabled(true) .config(RestAssured.config() .encoderConfig(encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT))); // 遍历参数动态添加multiPart for (Map.Entry<String, Object> entry : multipartParams.entrySet()) { requestSpec.multiPart(entry.getKey(), entry.getValue()); } Response response = requestSpec .config(RestAssured.config() .contentType(ContentType.JSON)) .headers(request.getHeaders()) .post(path); }
内容的提问来源于stack exchange,提问作者Automation Engr
相关产品推荐
相关产品推荐

