您的位置:首页 > 理论基础 > 计算机网络

常用网络接口自动化测试框架

2017-04-20 19:35 323 查看
一、RESTful(resource representational state transfer)类型接口测试

(一)GUI界面测试工具:jmeter

1、添加线程组





2、添加http请求





3、为线程组添加察看结果树





4、写入接口参数并运行





5、在查看结果树窗口查看结果





6、多组数据可增加CSVDataSetConfig(添加.csv格式的文件,并在参数值里以${x}格式写入)









此时变量值填写${变量名},上图x,y表示每次从文件里读取两个参数,分别命名为x,y

(二)JAVA语言脚本测试(HttpClient)

1、GET请求接口测试

public void TestGet throws URISyntaxException, ClientProtocolException, IOException{
  //1、创建一个客户端对象
3   CloseableHttpClient client=HttpClients.createDefault();
  //2、使用URIBuilder()来生成一个get类型的USI
  URI uri=new URIBuilder().setScheme("http")
                .setPort(8080)
                .setHost("localhost")
                .setPath("/test1334/Calc")
                .setParameter("a", "2")
                .setParameter("b", "3").build();
  //3、新建一个httpget类型请求对象,并将uri传入请求
  HttpGet get=new HttpGet(uri);
  //4、新建响应对象,用于接收客户端执行get结果
14  CloseableHttpResponse response=client.execute(get);
  //5.从响应对象中提取实际结果,与预期结果进行比对
  if(response.getStatusLine().getStatusCode()==200){
  System.out.println(EntityUtils.toString(response.getEntity()));
}
}


2、POST请求接口测试

样例(测一个输入两个参数求和的接口):

public void TestPOST () throws ClientProtocolException, IOException{
  //1.新建一个客户端对象
  CloseableHttpClient client=HttpClients.createDefault();
  //2.新建post类型请求对象,并传入uri
  HttpPost post = new HttpPost("http://172.31.6.155:8080/test1334/Calc");
  //3.使用NameValuePair对参数进行打包
  List<NameValuePair> list=new ArrayList<NameValuePair>();
  list.add(new BasicNameValuePair("a","1"));
  list.add(new BasicNameValuePair("b","2"));
  //4.对打包好的参数,使用UrlEncodedFormEntity工具类生成实体类型数据
  //Consts.UTF_8设置服务器字符集类型
  UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,Consts.UTF_8);
  //5.将含有请求参数的实体对象放入到post请求对象里
  post.setEntity(entity);
  //6.新建一个响应对象接收客户端执行post请求的结果
  CloseableHttpResponse response=client.execute(post);
  //7.从响应对象中提取实际结果,与预期结果进行比对
  if(response.getStatusLine().getStatusCode()==200){
    System.out.println(EntityUtils.toString(response.getEntity()));
}
}


3、自动化框架

@RunWith(Feeder.class)
public class getParameter {
@Test
@Source("data/datas.csv")    //数据源
public void test_get(int x,int y,int expect) throws ClientProtocolException, URISyntaxException, IOException{//expect为预期结果,用于与实际结果进行比对
TestRESTfultest=new TestRESTful();//TestRESTful为前边创建TestGet所属类
int returns=test.TestGet(x, y);//此处的为修改后的TestGet,添加了参数和返回值;
assertEquals(returns,expect); //将结果与预期进行比较
}
}


二、WebService接口测试

(一)GUI界面测试工具:SoapUI

1、新建项目

2、输入WSDL地址或文件

3、修改“?”内的数据

4、开始测试

(二)JAVA语言脚本测试(HttpClient)

1、GET请求接口测试

public int testGet(int x, int y) throws RemoteException {
String target = "http://172.31.6.94:8080/axis2/services/calc?wsdl";//传入地址
//创建一个CalcStub对象
4      CalcStub stub = new CalcStub(target);
CalcStub.Add add = new CalcStub.Add();
//传入参数
add.setX(x);
add.setY(y);
AddResponse response = stub.add(add);//结果
int result = response.get_return();
return result;
}


2、POST请求接口测试

public static void testPOST(int a,int b) throws ClientProtocolException, IOException{
//创建客户端对象
CloseableHttpClient cli=HttpClients.createDefault();
HttpPost po=new HttpPost("http://172.31.6.61:8080/axis2/services/MyService?wsdl");
//将soap协议内容添加进来,即soapXML字符串
String soapXML="<soapenv:Envelopexmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.day3.com\">"
+"<soapenv:Header/>"
+"<soapenv:Body>"
+"<ws:add>"
+"<ws:a>"+a+"</ws:a>"
+"<ws:b>"+b+"</ws:b>"
+"</ws:add>"
+"</soapenv:Body>"
+"</soapenv:Envelope>";
//将String转换成实体类型
StringEntity entity=new StringEntity(soapXML,Charset.forName("UTF-8"));
po.setEntity(entity);
CloseableHttpResponse re=cli.execute(po);
System.out.println((re.getEntity()).toString());
}


3、自动化框架(同RESTful的自动化测试;略)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: