基于Jmeter工具编写java测试脚本便于后期的接口测试用例的设计和维护。接口测试的脚本基本是复用的。可以在满足接口测试的同时还可以进行性能测试。 主要包括以下几个部分:
1、新建测试类,该类必须继承AbstractJavaSamplerClient类或实现JavaSamplerClient接口,
2、需要重写以下方法:
(1)`public Arguments getDefaultParameters()`;
//设置入参,已设置的参数会显示在jmeter GUI的参数列表中
(2)`public void setupTest(JavaSamplerContext context)`;
//初始化方法,用于初始化性能测试的每个线程,每个线程前都会执行一次。
(3)`public SampleResult runTest(JavaSamplerContext arg0);`
//性能测试的线程运行体,测试执行主体,从arg0中获取参数,并调用被测方法,完成与服务器的交互。该方法是java Sampler实现的重点,执行次数取决于线程数和循环次数。
(4)`public void teardownTest(JavaSamplerContext arg0);`
//测试结束时调用,每个线程执行一次。setupTest和teardownTest方法不需要时可以不写。
3、以上4个方法中只有runTest是必须实现的,其他3个可根据需求去覆盖。
这4个方法执行的先后顺序与其前面的序号相对应,分别 为:
getDefaultParameters();
setupTest(JavaSamplerContext context);
runTest(JavaSamplerContext arg0);
teardownTest(JavaSamplerContext arg0);
例子:此处以登录为例子:
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.util.HashMap;
import java.util.Map;
import static com.example.JmeterTest.JunitTestCase.BasicInfo.HOST_ADDR;
import static com.example.JmeterTest.util.MaptoJson.map2Json;
public class LoginTeseCase extends AbstractJavaSamplerClient {
private SampleResult sampleresult = new SampleResult();
private String userName;
private String password;
private String remember;
private String code;
private String message;
@Override
public Arguments getDefaultParameters(){
Arguments params = new Arguments();
params.addArgument("userName","");
params.addArgument("password","");
params.addArgument("remember","");
params.addArgument("code","");
params.addArgument("message","");
return params;
}
protected org.apache.log.Logger getLogger(){
return null;
}
public void setupTest(JavaSamplerContext context){
userName = context.getParameter("userName");
password = context.getParameter("password");
remember = context.getParameter("remember");
code = context.getParameter("code");
message = context.getParameter("message");
}
public SampleResult runTest(JavaSamplerContext context) {
userName = context.getParameter("userName");
password = context.getParameter("password");
remember = context.getParameter("remember");
code = context.getParameter("code");
message = context.getParameter("message");
sampleresult.sampleStart();
try{
StringBuilder urlBuff = new StringBuilder();
urlBuff.append(HOST_ADDR).append(RemoteServerEnum.userLogin.getServer());
System.out.println(urlBuff);
Map<String,String> params = new HashMap<>();
params.put("userName",userName);
params.put("password",password);
params.put("remember", remember);
String strJson = map2Json((HashMap<String, String>) params).toString();
System.out.println(strJson);
StringEntity requestEntity = new StringEntity(strJson, ContentType.create("text/json", "UTF-8"));
String respJson = HttpsClientUtils.doPostRequest
(urlBuff.toString(),null,null,requestEntity);
System.out.println(respJson);
sampleresult.setResponseData("结果是:"+respJson,"UTF-8");
sampleresult.setDataType(SampleResult.DEFAULT_HTTP_ENCODING);
sampleresult.setSuccessful(true);
}catch (Exception e){
sampleresult.setSuccessful(false);
e.printStackTrace();
}finally {
sampleresult.sampleEnd();
}
return sampleresult;
}
public void teardownTest(JavaSamplerContext context){ }
public static void main(String[] args) {
Arguments params = new Arguments();
params.addArgument("userName", "student");
params.addArgument("password", "123456");
params.addArgument("remember","true");
params.addArgument("code","0");
params.addArgument("message","successful");
JavaSamplerContext arg0 = new JavaSamplerContext(params);
LoginTeseCase test = new LoginTeseCase();
test.setupTest(arg0);
test.runTest(arg0);
test.teardownTest(arg0);
}
}
|