Cucumber+Rest Assured快速搭建api自动化测试平台
轉載:http://www.jianshu.com/p/6249f9a9e9c4
什么是Cucumber?什么是BDD?這里不細講,不懂的直接查看官方:https://cucumber.io/
什么是Rest Assured?傳送門:https://github.com/rest-assured/rest-assured
以下以java為開發語言,快速搭建一個cucumber+Rest Assured的api自動化測試平臺。
1. 用IDEA 新建一個Maven工程,并pom文件添加如下配置:
新建個ApiTools類,并對Rest Assured進程二次封裝,主要是Post 和 Get請求的基礎封裝和對返回json解析的封裝,具體代碼如下:
/*** 帶json的post請求** @param apiPath api地址* @param json 請求json* @return api返回的Response*/public static Response post(String apiPath, String json) { // 開始發起post 請求String path = Parameters.BOSEHOST + apiPath;Response response = given().contentType("application/json;charset=UTF-8").headers("header1", "value1").cookies("cookies1", "value1").body(json).when().log().all().post(path.trim());log.info(response.statusCode());log.info("reponse:");response.getBody().prettyPrint();return response;}/*** get 請求** @param apiPath api路徑* @return api的response*/public static Response get(String apiPath) { // 開始發起GET 請求String path = Parameters.BOSEHOST + apiPath;Response response = given().contentType("application/json;charset=UTF-8").headers("headers1", "value1").cookie("cookie1", "value1").when().log().all().get(path.trim());log.info(response.statusCode());log.info("reponse:");response.getBody().prettyPrint();return response;}/*** 獲取json中某個key值* @param response 接口返回* @param jsonPath jsonpath, 例如 a.b.c a.b[1].c a* @return*/public static String getJsonPathValue(Response response, String jsonPath) {String reponseJson = String.valueOf(response.jsonPath().get(jsonPath)); // String jsonValue = String.valueOf(from(reponseJson).get(jsonPath));return reponseJson;}3.新建個Steps 類,完成常用step的封裝,具體代碼如下:
import com.jayway.restassured.response.Response; import com.tools.apitools.ApiTools; import com.tools.apitools.MyAssert; import com.tools.filetools.ReadTxtFile; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; import static net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals; /** * Created by MeYoung on 8/1/2016. * <p> * Steps 集合 */ public class Steps {Response response = null;@When("^I send a GET request to \"(.*?)\"$")public void getRequest(String path) {response = ApiTools.get(path);}@When("^I send a POST request to \"(.*?)\"$")public void postRequest(String apiPath) throws Throwable {response = ApiTools.post(apiPath);}@When("^I send a POST request to \"(.*?)\" and request json:$")public void postRequestWithJson(String apiPath, String json) {response = ApiTools.post(apiPath, json);}@When("^I use a \"(.*?)\" file to send a POST request to \"(.*?)\"$")public void postRequestWihtFile(String fileName, String path) {String json = ReadTxtFile.readTxtFile(fileName);response = ApiTools.post(path, json);}@Then("^the JSON response equals$")public void assertResponseJson(String expected) {String responseJson = response.body().asString();assertJsonEquals(responseJson, expected);}@Then("^the JSON response equals json file \"(.*?)\"$")public void theJSONResponseEqualsJsonFile(String fileName) {String responseJson = response.body().asString();String fileJson = ReadTxtFile.readTxtFile(fileName);assertJsonEquals(responseJson, fileJson);}@Then("^the response status should be \"(\\d{3})\"$")public void assertStatusCode(int statusCode) {Object jsonResponse = response.getStatusCode();MyAssert.assertEquals(jsonResponse, statusCode);}@Then("^the JSON response \"(.*?)\" equals \"(.*?)\"$")public void assertEquals(String str, String expected) {String jsonValue = ApiTools.getJsonPathValue(response, str);MyAssert.assertEquals(jsonValue, expected);}@Then("^the JSON response \"(.*?)\" type should be \"(.*?)\"$")public void assertMatch(String str, String match) {String jsonValue = ApiTools.getJsonPathValue(response, str);MyAssert.assertMatch(jsonValue, match);}@Then("^the JSON response \"(.*?)\" should be not null$")public void assertNotNull(String str) {String jsonValue = ApiTools.getJsonPathValue(response, str);MyAssert.assertNotNull(jsonValue);}@Then("^the JSON response \"(.*?)\" start with \"(.*?)\"$")public void assertStartWith(String str, String start) {String jsonValue = ApiTools.getJsonPathValue(response, str);MyAssert.assertStartWith(jsonValue, start);}@Then("^the JSON response \"(.*?)\" end with \"(.*?)\"$")public void assertEndWith(String str, String end) {String jsonValue = ApiTools.getJsonPathValue(response, str);MyAssert.assertEndWith(jsonValue, end);}@Then("^the JSON response \"(.*?)\" include \"(.*?)\"$")public void assertInclude(String str, String include) {String jsonValue = ApiTools.getJsonPathValue(response, str);MyAssert.assertInclude(jsonValue, include);} }當然上面代碼還涉及到一些Asssert的封裝,這里就不列出來,個人喜好不同,更具自己熟悉的情況去引入自己熟悉的jar包。
ok,最后我們愉快的寫兩個case,看看效果:
@getScenario Outline: use examplesWhen I send a GET request to "apiurl"Then the response status should be "200"Then the JSON response "<jsonPath>" equals "<value>"Examples:| jsonPath | value || genericPlan | false || ehiCarrierId | 90121100 || carrierName | Anthem Blue Cross |@postScenario: test post requestWhen I send a POST request to "apiurl"Then the response status should be "200"And the JSON response "message" equals "success"# 校驗放回值是否是某種類型And the JSON response "sessionId" type should be "^\d{6}$"# 校驗返回值不為空And the JSON response "url" should be not null# 校驗是否以XX開頭Then the JSON response "message" start with "su"# 校驗是否以XX開頭Then the JSON response "message" end with "ss"# 校驗是否以XX開頭Then the JSON response "message" include "ss"# 校驗返回json是否為XXX,對整個返回json的校驗Then the JSON response equals"""{"result":"success"}"""通過Junit 運行feature.
在Pom.xml 文件添加junit相關包:
<dependency><groupId>info.cukes</groupId><artifactId>cucumber-junit</artifactId><version>1.2.4</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>@RunWith(Cucumber.class) : 注解表示通過Cucumber的Junit 方式運行腳本
@CucumberOptions () :注解用于配置運行信息,其中代碼中的plugin 表示測試報告輸出的路徑和格式, feature 表示被運行的feature文件的包路徑, glue中配置steps的包路徑地址,tags中配置要運行的用例的tags名,其實~符號表示除了這個tags的所有tags.
通過Jenkins 執行
在Pom.xml 文件里面添加運行插件,如下:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.3</version><inherited>true</inherited><configuration><source>1.7</source><target>1.7</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.18.1</version><configuration><reuseForks>false</reuseForks></configuration></plugin></plugins></build>Paste_Image.png
Cucumber-JVM reports 提供了非常漂亮的report,如下:
Paste_Image.png
最后附上項目地址:https://github.com/MeYoung/cucumber_restassured
作者:米陽MeYoung
鏈接:http://www.jianshu.com/p/6249f9a9e9c4
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。 參考:https://github.com/jenkinsci/cucumber-reports-plugin
轉載于:https://www.cnblogs.com/ceshi2016/p/7481527.html
總結
以上是生活随笔為你收集整理的Cucumber+Rest Assured快速搭建api自动化测试平台的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android四大组件之Service
- 下一篇: ViewStub你肯定听过,但是这些细节