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

一、RESTful(resource representational state transfer)类型接口测试

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

1、添加线程组

clip_image002

2、添加http请求

clip_image004

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

clip_image006

4、写入接口参数并运行

clip_image008

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

clip_image010

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

clip_image012

clip_image014

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

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

1、GET请求接口测试

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

2、POST请求接口测试

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

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

3、自动化框架

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

二、WebService接口测试

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

1、新建项目

2、输入WSDL地址或文件

3、修改“?”内的数据

4、开始测试

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

1、GET请求接口测试

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

2、POST请求接口测试

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

3、自动化框架(同RESTful的自动化测试;略)

原文地址:https://www.cnblogs.com/aland-1415/p/6740466.html