metersphere全局脚本断言

本文探讨了在接口测试中如何有效地实现全局断言,以简化多个接口的断言过程。通过检查响应消息中是否包含特定成功信息,以及响应码是否为200,来判断接口调用是否成功。提供了两种不同的实现方式,一种是基于消息内容的断言,另一种是基于响应码的断言。这些方法有助于提高测试效率并确保测试覆盖率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景:每个接口设置断言会比较麻烦,如果使用全局进行断言,但是出现操作成功、保存成功、删除成功等这种不同接口返回,如果实现一个全局接口去断言呢?

Failure = false;-----表示断言成功,

FailureMessage = "……"; ----自定义的成功信息

Failure = true;-----表示断言失败,

FailureMessage = "……";-----自定义的失败信息。

上脚本

方法一:

import org.json.*;

//获取上一个请求的返回
String jsonString = prev.getResponseDataAsString();
JSONObject responseJson = new JSONObject(jsonString);

String fbpcontent = responseJson.getString("message");



value = fbpcontent;
result = value.contains("成功");
if (!result){
    
	msg = "assertion [" + value + " contains '成功']: false;";
	AssertionResult.setFailureMessage(msg);
	AssertionResult.setFailure(true);
}

额外参考1

String response = prev.getResponseDataAsString();
String code = prev.getResponseCode();
log.info("Respnse is " + response);
log.info(code);
int result1 = response.indexOf("The wait operation timed out");
int result2 = response.indexOf("Oops. Something went wrong ... sorry");
if(code == "200" && result1>=0 || result2>=0){
    FailureMessage = "OK,check current";
}
else{
    Failure = true;
    FailureMessage = "ERROR,check error";
}

额外参考2

String code = prev.getResponseCode();
log.info(code);




if ("200".equals(""+code) == false )
{
    // 响应码不等于200时,设置断言失败,并输出失败信息
    Failure=true ;
    FailureMessage ="Response code was not a 200 response code it was " + code + "." ;
    print ( "the return code is " + code);   // this goes to stdout
    log.warn( "the return code is " + code); // this goes to the JMeter log file
} else {
    // 响应码等于200时,设置断言成功,并输出成功信息
    Failure=false;
    FailureMessage = "Return true, and the response code was " + code;
     }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春天的菠菜

一毛两毛也是动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值