資深老鳥整理,Java接口自動化測試總結,從0到1自動化...

這幾年接口自動化變得越來越熱門,相對比于UI自動化,接口自動化有一些優勢

1)運行比UI更穩定,讓BUG更容易定位 2)UI自動化維護成本太高,接口相對低一些

接口測試其實有很多方式,主要有兩種,一個是工具,最常見的有:Postman,SoupUI,Jmeter;另一個就是代碼,Java和Python都可以實現。

工具的好處就是直觀,快速上手,有些工具也做到了半自動化和集成,但是工具還是會有一定的限制,代碼相對與工具來說是更萬能,利用接口測試框架結合TestNG或者Junit,實現接口自動化。

1、REST Assured測試框架

maven坐標

<dependencies><!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured --><dependency><groupId>io.rest-assured</groupId><artifactId>rest-assured</artifactId><version>4.0.0</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/io.rest-assured/json-path --><dependency><groupId>io.rest-assured</groupId><artifactId>json-path</artifactId><version>4.0.0</version></dependency><!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator --><dependency><groupId>io.rest-assured</groupId><artifactId>json-schema-validator</artifactId><version>4.0.0</version></dependency></dependencies>

官方文檔中建議靜態導入

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;

語法格式

public void testExample()
{given().queryParam("wd","mp3").when().get("http://www.baidu.com/s").then().log().all().statusCode(200);
}

given() 后面跟一次網絡請求所需要的條件

.cookies() --cookies 是Map形式存儲 .contentType() .queryParam("key","value") 用于get請求參數 .body(Jsondata) 存放Json格式類型 .body(XMLdata) 存放XML格式類型 .formParam("Key","Value") 表單參數類型 .multipartFile(new File(filePath)) .log().all() 打印所有日志 .relaxedHTTPSValidation() --處理無效SSL證書過期 不對的異常

when() 觸發條件

.get("url") .post("url") .post("url/{key1}/{key2}",value1,value2)

then() 斷言

.statusCode(200) .body("key",hasItems(""))

public Response testDemo(String corpid,String corpsecret ){Response res = RestAssured.given().log().all().when().get("https://baidu.com").then().extract().response();    return res;}

extract().response() 以response格式獲取響應結果 res.getCookie() res.getHeader() res.getStatusCode() res.path("").toString() 獲取返回中某個節點的值 res.asString() 獲取返回內容體

2、HttpClient

maven坐標

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.5</version>
</dependency>
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.4</version>
</dependency>

導入

import org.apache.http.HttpResponse;  
import org.apache.http.client.HttpClient;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.HttpEntity;

簡單實例

public String demoPost(String httpUrl) {String cookie ="JESSIONID=xxxxxxxx";      String params = "JsonData";StringEntity stringEntity = new StringEntity(params, "UTF-8");  stringEntity.setContentType("application/x-www-form-urlencoded");  HttpPost httpPost = new HttpPost(httpUrl);    httpPost.setHeader(cookie,cookie);httpPost.setEntity(stringEntity);  HttpClient client = new DefaultHttpClient();HttpResponse Response = client.execute(httpPost); String result = EntityUtils.toString(Response.getEntity());}

登錄需要存儲Cookie的方式

CookieStore cookiestore=new BasicCookieStore();
CloseableHttpClient client1=HttpClients.custom().setDefaultCookieStore(cookiestore).build();List<Cookie> cookies = cookiesstore.getCookies();

如果登錄時頁面有重定向操作,也可以用cookieStore的方式存儲每次重定向時需要用到的cookie

3、對返回的內容Json化

1)JSON

maven坐標

<dependency><groupId>org.json</groupId><artifactId>json</artifactId><version>20180130</version></dependency>

數組形式response

JSONArray array = new JSONArray(response);
JSONObject object = array.getJSONObject(0);
String value = object.get("key").toString();

帶頭信息的response

JSONObject object = new JSONObject(response);
String value = object.getString("key");

2)gson (推薦)

<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.2</version>  //如果使用更新的版本,JsonParser會被推薦不使用
</dependency>import com.google.gson.JsonParser;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

數組形式response

JsonPraser parser = new JsonParser();
JsonArray array = parser.parse(response).getAsJsonArray;
JsonObject object = array.get(0).getAsJsonObject();
String value = object.get("key").getAsString();

帶頭信息的response

JsonPraser parser = new JsonParser();
JsonObject object = parser.parse(response).getAsJsonOjbect();
JsonObject object_in = object.get("key").getAsJsonObject();
String value = object_in.get("key").getAsString();

getAsString() 獲取字符串的值 toString() 獲取字符串

還可以利用Gson提供的fromJson()方法來實現從Json相關對象到Java實體

import com.google.gson.Gson;Gson gson = new Gson();
JsonObject object = gson.fromJson(response,JsonObject.class);

上述例子將Json字符串轉化為了JsonObject實體

也可以將Json字符串轉化為自己寫的類

?總結

如果你對此文有任何疑問,如果你也需要接口項目實戰,如果你對軟件測試、接口測試、自動化測試、面試經驗交流感興趣歡迎加入我們,加入方式在文章的最后面

??自動化測試相關教程推薦:

2023最新自動化測試自學教程新手小白26天入門最詳細教程,目前已有300多人通過學習這套教程入職大廠!!_嗶哩嗶哩_bilibili

2023最新合集Python自動化測試開發框架【全棧/實戰/教程】合集精華,學完年薪40W+_嗶哩嗶哩_bilibili

測試開發相關教程推薦

2023全網最牛,字節測試開發大佬現場教學,從零開始教你成為年薪百萬的測試開發工程師_嗶哩嗶哩_bilibili

postman/jmeter/fiddler測試工具類教程推薦

講的最詳細JMeter接口測試/接口自動化測試項目實戰合集教程,學jmeter接口測試一套教程就夠了!!_嗶哩嗶哩_bilibili

2023自學fiddler抓包,請一定要看完【如何1天學會fiddler抓包】的全網最詳細視頻教程!!_嗶哩嗶哩_bilibili

2023全網封神,B站講的最詳細的Postman接口測試實戰教學,小白都能學會_嗶哩嗶哩_bilibili

??總結:

?光學理論是沒用的,要學會跟著一起敲,要動手實操,才能將自己的所學運用到實際當中去,這時候可以搞點實戰案例來學習。

如果對你有幫助的話,點個贊收個藏,給作者一個鼓勵。也方便你下次能夠快速查找。

如有不懂還要咨詢下方小卡片,博主也希望和志同道合的測試人員一起學習進步

在適當的年齡,選擇適當的崗位,盡量去發揮好自己的優勢。

我的自動化測試開發之路,一路走來都離不每個階段的計劃,因為自己喜歡規劃和總結,

測試開發視頻教程、學習筆記領取傳送門!!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390130.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390130.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390130.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

parcel react_如何使用Parcel設置React應用

parcel reactFor a long time Webpack was one of the biggest barriers-to-entry for someone wanting to learn React. Theres a lot of boilerplate configuration that can be confusing, especially if youre new to React. 長期以來&#xff0c; Webpack一直是想要學習Re…

1117. H2O 生成

1117. H2O 生成 現在有兩種線程&#xff0c;氧 oxygen 和氫 hydrogen&#xff0c;你的目標是組織這兩種線程來產生水分子。 存在一個屏障&#xff08;barrier&#xff09;使得每個線程必須等候直到一個完整水分子能夠被產生出來。 氫和氧線程會被分別給予 releaseHydrogen 和…

pdf 字體和圖片抽取

2019獨角獸企業重金招聘Python工程師標準>>> #安裝mutoos sudo apt-get install mupdf-tools #抽取字體 mutool extract LTN20180531052_C.pdf 轉載于:https://my.oschina.net/colin86/blog/1842412

推箱子2-向右推!_保持冷靜,砍箱子-銀行

推箱子2-向右推!Hack The Box (HTB) is an online platform allowing you to test your penetration testing skills. It contains several challenges that are constantly updated. Some of them are simulating real world scenarios and some of them lean more towards a …

611. 有效三角形的個數

611. 有效三角形的個數 給定一個包含非負整數的數組&#xff0c;你的任務是統計其中可以組成三角形三條邊的三元組個數。 示例 1: 輸入: [2,2,3,4] 輸出: 3 解釋: 有效的組合是: 2,3,4 (使用第一個 2) 2,3,4 (使用第二個 2) 2,2,3注意: 數組長度不超過1000。數組里整數的范…

python學習day04

一&#xff1a;今天是一個學習列表后的實踐訓練 購物小程序&#xff1a; #codeing:UTF-8 #__author__:Duke #date:2018/3/1/001product_list [(mac,7000),(bike,1000),(phone,2000),(kindle,800),(iwatch,3000), ]; shopping_car []; saving input("please input your …

mybatis多產數_freeCodeCamp杰出貢獻者–我們如何選擇,認可和獎勵多產的志愿者

mybatis多產數freeCodeCamp.org is only possible thanks to the thousands of contributors around the world who help expand and improve the community. They do this mainly through:感謝全球各地成千上萬的貢獻者&#xff0c;他們致力于擴大和改善社區&#xff0c;因此f…

502. IPO

502. IPO 假設 力扣&#xff08;LeetCode&#xff09;即將開始 IPO 。為了以更高的價格將股票賣給風險投資公司&#xff0c;力扣 希望在 IPO 之前開展一些項目以增加其資本。 由于資源有限&#xff0c;它只能在 IPO 之前完成最多 k 個不同的項目。幫助 力扣 設計完成最多 k 個…

記一次打包的詭異現象

一、前情提要&#xff1a; 今天線上打包&#xff0c;發現啟動正常&#xff0c;但是訪問異常&#xff0c;看日志也沒有打印出什么異常信息。 更新的微服務包訪問的時候一直報出【403】&#xff0c;訪問被拒 項目架構&#xff1a;springBoot maven 二、機緣巧合&#xff1a; 上午…

轉載:mysql存儲過程講解

記錄MYSQL存儲過程中的關鍵語法&#xff1a; DELIMITER // 聲明語句結束符&#xff0c;用于區分; CEATE PROCEDURE demo_in_parameter(IN p_in int) 聲明存儲過程 BEGIN …. END 存儲過程開始和結束符號 SET p_in1 變量賦值 DECLARE l_int int unsigned default 4000000; 變…

Diffie-Hellman:安全網絡通信背后的天才算法

Lets start with a quick thought experiment.讓我們從快速思考實驗開始。 You have a network of 3 computers, used by Alice, Bob, and Charlie. All 3 participants can send messages, but just in a way that all other clients who connected to the network can read …

掃盲丨關于區塊鏈你需要了解的所有概念

掃盲丨關于區塊鏈你需要了解的所有概念 如今存儲信息的方式有什么問題&#xff1f; 目前&#xff0c;支配我們生活的數據大部分都儲存在一個地方&#xff0c;不論是在私人服務器、云、圖書館或檔案館的紙上。大多數情況下這很好&#xff0c;但這也容易受到攻擊。 最近有消息稱&…

SpringBoot環境切換

2019獨角獸企業重金招聘Python工程師標準>>> 1.在application.yml中配置&#xff0c;如果java -jar banke-boot-bd-api-0.0.1-SNAPSHOT.jar&#xff0c;那么就是已application-test作為啟動的配置文件啟動 spring: profiles: active: test 2.如果java -jar banke-bo…

linux tar cvf_Linux中的Tar命令:Tar CVF和Tar XVF通過示例命令進行了解釋

linux tar cvfThe name tar is, by most accounts, short for tape archive. The "tapes" in question would be all those magnetic storage drives that were popular all the way back in the 1950s. 在大多數情況下&#xff0c; tar是磁帶歸檔的縮寫。 有問題的“…

1894. 找到需要補充粉筆的學生編號

1894. 找到需要補充粉筆的學生編號 一個班級里有 n 個學生&#xff0c;編號為 0 到 n - 1 。每個學生會依次回答問題&#xff0c;編號為 0 的學生先回答&#xff0c;然后是編號為 1 的學生&#xff0c;以此類推&#xff0c;直到編號為 n - 1 的學生&#xff0c;然后老師會重復…

[No0000B0]ReSharper操作指南1/16-入門與簡介

安裝指南 在安裝之前&#xff0c;您可能需要檢查系統要求。 ReSharper是一個VisualStudio擴展。它支持VisualStudio2010,2012,2013,2015和2017.安裝完成后&#xff0c;您將在VisualStudio的主菜單中找到新的ReSharper條目。大多數ReSharper命令都可以在這個菜單中找到。但是&a…

更改H2元素的顏色

In coding there are often many different solutions to a given problem. This is especially true when it comes to styling an HTML element.在編碼中&#xff0c;對于給定問題通常有許多不同的解決方案。 在樣式化HTML元素時&#xff0c;尤其如此。 One of the easiest …

[CTSC2008]圖騰totem

&#xff08;圖騰這題做的我頭疼 233&#xff09; 記 f(xxxx) 為 xxxx 出現的次數&#xff0c;那么題目就是要求 f(1324) - f(1243) - f(1432) 最有難度的是把上面的式子轉化一下&#xff0c;變成 f(1x2x) - f(14xx) - f(12xx) f(1234) 這點除非對 f 的求法能一眼看出來&#…

Box Shadow CSS教程–如何向任何HTML元素添加投影

We can add a drop shadow to any HTML element using the CSS property box-shadow. Heres how. 我們可以使用CSS屬性box-shadow將陰影添加到任何HTML元素。 這是如何做。 添加基本??投影 (Adding a Basic Drop Shadow) Lets first set up some basic HTML elements to add…

數據結構學習筆記(一)——《大話數據結構》

第一章 數據結構緒論 基本概念和術語 數據 描述客觀事物的符號&#xff0c;計算機中可以操作的對象&#xff0c;能被計算機識別并輸入給計算機處理的符號的集合。包括整型、實型等數值類型和字符、聲音、圖像、視頻等非數值類型。 數據元素 組成數據的、有一定意義的基本單位&a…