常用網絡接口自動化測試框架


 

一、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的自動化測試;略)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM