2018-04-22接口自动化测试学习心得(1)
根據接口文檔寫接口測試用例-->添加接口自動化測試項目相關依賴(httpclient+testng+poi-ooxml+log4j+mail+mysql-connector-java)-->寫接口測試方法-->執行測試
-- 接口測試
1.一個接口就是一個函數
2.我們要保證一個接口能夠在url地址欄里面訪問到,必須滿足一下兩個條件
一是這個接口首先是部署到服務器上的web容器中,
而是此接口必須實現了http協議
-- 接口文檔
接口地址
請求方式
參數
變量名
類型
說明
備注
-- 接口測試工具:
soapui,jmeter,pastman,fiddler
都操作一次
soapui:準備url創建項目,根據接口文檔確定提交的方式,準備傳參,發送請求,獲取接口返回數據
-- ui層面的功能測試跟接口測試的區別:
1.ui層面可以通過頁面填寫數據,傳數據到后臺,
但是接口測試沒有頁面,我們只能借助接口測試工具模擬頁面去提交接口數據。
2.UI層面的功能測試會有后臺接口返回的數據,并且數據會通過js以提示信息的方式顯示在頁面幫助用戶或者測試人員去判斷功能是否正常。
但是接口測試就沒有頁面提示信息,我們能看到的是接口返回的最原始的數據。
接口自動化實現:
java有專門的第三方框架可以提供了一套基于http協議的接口請求和處理技術。--httpclient
-- 環境搭建
創建maven項目-eclipse集成testng-集成testng框架
添加依賴
?? ?<dependencies>
?? ??? ?<!-- 管理用例,提供多種測試場景實現 -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.testng</groupId>
?? ??? ??? ?<artifactId>testng</artifactId>
?? ??? ??? ?<version>6.8.8</version>
?? ??? ??? ?<scope>test</scope>
?? ??? ?</dependency>
?? ??? ?<!-- 接口提交(get/post) -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.apache.httpcomponents</groupId>
?? ??? ??? ?<artifactId>httpclient</artifactId>
?? ??? ??? ?<version>4.5.2</version>
?? ??? ?</dependency>
?? ??? ?<!-- 解析excel文件 -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.apache.poi</groupId>
?? ??? ??? ?<artifactId>poi-ooxml</artifactId>
?? ??? ??? ?<version>3.15</version>
?? ??? ?</dependency>
?? ??? ?<!-- 記錄日志框架 -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>log4j</groupId>
?? ??? ??? ?<artifactId>log4j</artifactId>
?? ??? ??? ?<version>1.2.17</version>
?? ??? ?</dependency>
?? ??? ?<!-- 發送郵件框架 -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>javax.mail</groupId>
?? ??? ??? ?<artifactId>mail</artifactId>
?? ??? ??? ?<version>1.5.0-b01</version>
?? ??? ?</dependency>
?? ??? ?<!-- jdbc技術,數據庫驅動 -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>mysql</groupId>
?? ??? ??? ?<artifactId>mysql-connector-java</artifactId>
?? ??? ??? ?<version>5.1.38</version>
?? ??? ?</dependency>
?? ?</dependencies>
-- httpPost
1,以post方式提交請求到服務器
2,參數封裝到請求體當中
3,數據長度是沒有限制的
4,支持post方式提交的接口往往是吧數據提交到服務器
關鍵api函數
構造函數:
HttpPost post=new HttpPost(url);
創建一個post對象,以post方式提交接口請求
設置參數到請求體
post.setEntity(new UrlEncodedFormEntity(params));
通過此方法將接口參數設置到請求中
-- httpGet
1,以get方式提交請求到服務器
2,get參數不是封裝在請求體當中的(由沒有setEntify方法就可以看出)
3,數據長度是有限制的
4,支持get提交的接口一般都是從服務器拿下來數據
關鍵api函數
構造函數:
HttpGet httpGet=new HttpGet(interfacesurl);
創建一個get對象,以get方式提交接口請求
注意:如果以get提交的接口請求有需要傳參,蠶食通常是直接拼接在url后面
例子
public class RegisterInterface {
?? ?@Test
?? ?public void test1() throws Exception{
?? ??? ?//準備url;
?? ??? ?String url="http://8080/lmcanon_web_auto/mvc/member/api/member/register";
?? ??? ?//接口提交的方式;
?? ??? ?HttpPost post=new HttpPost(url);
?? ??? ?//準備傳參;
?? ??? ?List<NameValuePair>params=new ArrayList<NameValuePair>();
?? ??? ?params.add(new BasicNameValuePair("mobilephone", "13517315121"));
?? ??? ?params.add(new BasicNameValuePair("pwd", "e10adc3949ba59abbe56e057f20f883e"));
?? ??? ?//將參數設置到post請求中
?? ??? ?post.setEntity(new UrlEncodedFormEntity(params));
?? ??? ?//發送請求;
?? ??? ?CloseableHttpClient httpclient=HttpClients.createDefault();
?? ??? ?CloseableHttpResponse response=httpclient.execute(post);
?? ??? ?//獲取接口返回數據.
?? ??? ?String jsonStr=EntityUtils.toString(response.getEntity());
?? ??? ?System.out.println(jsonStr);
?? ?}}
?? ?
?? ?@Test
?? ?public void get() throws Exception{
?? ??? ?//定義接口地址
?? ??? ?String interfacesurl="http://8080/futureload/mvc/api/member/list";
?? ??? ?//決定接口提交方式
?? ??? ?HttpGet httpGet=new HttpGet(interfacesurl);
?? ??? ?//直接提交接口請求,準備客戶端
?? ??? ?CloseableHttpClient httpClient=HttpClients.createDefault();
?? ??? ?CloseableHttpResponse response=httpClient.execute(httpGet);
?? ??? ?//獲取相應數據
?? ??? ?String jsonString=EntityUtils.toString(response.getEntity());
?? ??? ?System.out.println(jsonString);
?? ?}}
?? ?
--httpRequeste
接口類型,表示是從客戶端發送服務端請求
HttpGet和httpPost都是httpRequest實現類,屬于子類對象
-- httpResponse?? ?
接口類型,表示是從服務端到客戶端的響應
從響應對象中獲取返回數據:getEntity()
從響應對象中獲取響應狀態:getStatusLine()
從響應對象中獲取響應頭信息:getAllHeaders()
從響應對象中設置響應頭信息:setHeader
-- 93框架實現步驟
1,testng管理用例
2,實現httpUtil類提供公共的方法處理post和get請求
3,設計測試用例
4,實現數據驅動(獲取url,接口類型,傳參,封裝成方法)
5,接口測試數據回寫
6,日志集成
7,報表集成
8,jenkins集成
--get和post公共方法
public static String getResultStringByPost(String uri,List<NameValuePair>params){
?? ??? ?//調用函數StringUtils.isEmpty(uri),對uri是否為空的處理
?? ??? ?if(StringUtils.isEmpty(uri)){
?? ??? ??? ?return "";
?? ??? ??? ?}
?? ??? ?//創建httpPost對象
?? ??? ?HttpPost httpPost=new HttpPost(uri);
?? ??? ?String resultString="";
?? ??? ?//設置參數到httpPost對象中
?? ??? ?try {
?? ??? ??? ?httpPost.setEntity(new UrlEncodedFormEntity(params));
?? ??? ??? ?//準備httpclient客戶端
?? ??? ??? ?CloseableHttpClient httpClient= HttpClients.createDefault();
?? ??? ??? ?//發送請求
?? ??? ??? ?CloseableHttpResponse response=httpClient.execute(httpPost);
?? ??? ??? ?//解析數據
?? ??? ??? ?resultString=EntityUtils.toString(response.getEntity());
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return resultString;
?? ??? ?
?? ?}
?? ?/**get請求通用處理函數
?? ? * @param uri:接口地址
?? ? * @param params:提交參數
?? ? * @return:接口響應數據
?? ? */
?? ?public static String getResultStringByGet(String uri,List<NameValuePair>params){
?? ??? ?//調用函數StringUtils.isEmpty(uri),對uri是否為空的處理
?? ??? ?if(StringUtils.isEmpty(uri)){
?? ??? ??? ?return "";
?? ??? ??? ?}
?? ??? ?if(params==null){
?? ??? ??? ?return "";
?? ??? ?}
?? ??? ?for (int i=0;i<params.size();i++) {
?? ??? ??? ?NameValuePair nameValuePair=params.get(i);
?? ??? ??? ?if(i==0){
?? ??? ??? ?uri+=("?"+nameValuePair.getName()+"="+nameValuePair.getValue());
?? ??? ??? ?}else{
?? ??? ??? ??? ?uri+=("&"+nameValuePair.getName()+"="+nameValuePair.getValue());
?? ??? ??? ?}
?? ??? ?}
?? ??? ?//創建httpGet對象
?? ??? ?HttpGet httpGet=new HttpGet(uri);
?? ??? ?String resultString="";
?? ??? ?//設置參數到httpPost對象中
?? ??? ?try {
//?? ??? ??? ?httpGet.setEntity(new UrlEncodedFormEntity(params));
?? ??? ??? ?//準備httpclient客戶端
?? ??? ??? ?CloseableHttpClient httpClient= HttpClients.createDefault();
?? ??? ??? ?//發送請求
?? ??? ??? ?CloseableHttpResponse response=httpClient.execute(httpGet);
?? ??? ??? ?//解析數據
?? ??? ??? ?resultString=EntityUtils.toString(response.getEntity());
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return resultString;
?? ??? ?
?? ?}
//StringUtils判空方法
public class StringUtils {
?? ?public static boolean isEmpty(String content){
?? ??? ?boolean isEmpty=(content==null||content!=null&&content.trim().length()==0);
?? ??? ?return isEmpty;
?? ?}
}
轉載于:https://www.cnblogs.com/AIME/p/8907207.html
總結
以上是生活随笔為你收集整理的2018-04-22接口自动化测试学习心得(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《软件需求十步走》阅读笔记06
- 下一篇: python——函数 11、命名空间