js rest,rest教程_REST保證教程

 2023-11-19 阅读 16 评论 0

摘要:rest教程REST Assured is a Java Domain Specific Language API for simplifying testing of RESTful web services. REST Assured API can be used to invoke REST web services and match response content to test them. REST Assured是Java域特定語言API,用于簡化R

rest教程

REST Assured is a Java Domain Specific Language API for simplifying testing of RESTful web services. REST Assured API can be used to invoke REST web services and match response content to test them.

REST Assured是Java域特定語言API,用于簡化RESTful Web服務的測試。 REST安全的API可用于調用REST Web服務并匹配響應內容以對其進行測試。

放心 (REST Assured)

REST Assured can be used to test XML as well as JSON based web services. REST Assured can be integrated with JUnit and TestNG frameworks for writing test cases for our application.

js rest, REST Assured可用于測試XML以及基于JSON的Web服務。 REST Assured可以與JUnit和TestNG框架集成,以便為我們的應用程序編寫測試用例。

REST Assured supports POST, GET, PUT, DELETE, OPTIONS, PATCH, and HEAD requests and can be used to validate and verify the response of these requests.

REST Assured支持POST,GET,PUT,DELETE,OPTIONS,PATCH和HEAD請求,可用于驗證和驗證這些請求的響應。

REST Assured is implemented in Groovy and uses the builder pattern to create requests, set headers, parse the response and then match them with expected data. It uses Hamcrest Matchers for comparing actual response with the expected response.

REST Assured在Groovy中實現,并使用構建器模式創建請求,設置標頭,解析響應,然后將其與預期數據匹配。 它使用Hamcrest Matchers將實際響應與預期響應進行比較。

rest請求、One of the powerful features of REST assured is the support of XML Path and JSON Path syntax to check specific elements of the response data. It’s very similar to using XPath API.

REST確保的強大功能之一是對XML Path和JSON Path語法的支持,以檢查響應數據的特定元素。 這與使用XPath API非常相似。

REST保證教程先決條件 (REST Assured Tutorial Prerequisites)

Before we create our REST Assured tests, we need some web services to test. Below are the additional components used in this tutorial.

在創建REST保證測試之前,我們需要一些Web服務進行測試。 以下是本教程中使用的其他組件。

  1. JSON Server: It’s a great tool to create mock JSON based web services, all it requires is a sample JSON file. It automatically creates GET, POST, PUT, DELETE API endpoints for us. I have written about it at JSON Server tutorial. Below JSON is the input for our example JSON based web service.
    {"employees": [{"id": 1,"name": "Pankaj","salary": "10000"},{"name": "David","salary": "5000","id": 2}]
    }

    Below image shows the APIs exposed by json-server mock web service.

    JSON Server :這是一個創建基于JSON的模擬Web服務的好工具,它所需要的只是一個示例JSON文件。 它會自動為我們創建GET,POST,PUT,DELETE API端點。 我已經在JSON Server教程中對此進行了介紹。 JSON下面是基于示例JSON的Web服務的輸入。
    {"employees": [{"id": 1,"name": "Pankaj","salary": "10000"},{"name": "David","salary": "5000","id": 2}]
    }

    rest開發, 下圖顯示了json-server模擬Web服務公開的API。

  2. Jersey: I am using XML based web service created in Jersey Tutorial. You can download this project from our GitHub Repository and run on tomcat.

    澤西島 :我正在使用在澤西島教程中創建的基于XML的Web服務。 您可以從我們的GitHub存儲庫下載該項目并在tomcat 上運行。
  3. TestNG: I will use TestNG to create test cases with REST Assured, you can use JUnit too. You can learn about TestNG framework through our TestNG Tutorials.

    TestNG :我將使用TestNG通過REST Assured創建測試用例,您也可以使用JUnit。 您可以通過我們的TestNG教程了解TestNG框架。

REST保證教程 (REST Assured Tutorial)

Create a maven based project in Eclipse and add Rest Assured and TestNG dependencies.

在Eclipse中創建一個基于Maven的項目,并添加Rest Assured和TestNG依賴項。

<dependency><groupId>io.rest-assured</groupId><artifactId>rest-assured</artifactId><version>3.1.0</version><scope>test</scope>
</dependency>
<dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>6.14.3</version><scope>test</scope>
</dependency>

REST保證GET測試 (REST Assured GET Test)

Below code snippet shows how to call GET method and test response JSON elements. Notice the use of static imports, some of them will be used in subsequent examples.

下面的代碼片段顯示了如何調用GET方法和測試響應JSON元素。 注意使用靜態導入 ,其中一些將在后續示例中使用。

import static io.restassured.RestAssured.delete;
import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.hasItems;import org.hamcrest.Matchers;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;import io.restassured.http.ContentType;
import io.restassured.response.Response;public class RESTAssuredJSONTests {final static String ROOT_URI = "https://localhost:7000/employees";@Test
public void simple_get_test() {Response response = get(ROOT_URI + "/list");System.out.println(response.asString());response.then().body("id", hasItems(1, 2));response.then().body("name", hasItems("Pankaj"));
}
}

rest框架?Here is another complex example, where we are using TestNG DataProvider with REST Assured.

這是另一個復雜的示例,其中我們將TestNG DataProvider與REST Assured一起使用。

@Test(dataProvider = "dpGetWithParam")
public void get_with_param(int id, String name) {get(ROOT_URI + "/get/" + id).then().body("name", Matchers.is(name));
}@DataProvider
public Object[][] dpGetWithParam() {Object[][] testDatas = new Object[][] { new Object[] { 1, "Pankaj" },new Object[] { 2, "David" } };return testDatas;
}

REST保證POST示例 (REST Assured POST Example)

Below code snippet shows how to create JSON Request with different headers, then test the response elements.

下面的代碼片段顯示了如何使用不同的標頭創建JSON請求,然后測試響應元素。

@Test
public void post_test() {Response response = given().contentType(ContentType.JSON).accept(ContentType.JSON).body("{\"name\": \"Lisa\",\"salary\": \"2000\"}").when().post(ROOT_URI + "/create");System.out.println("POST Response\n" + response.asString());// testsresponse.then().body("id", Matchers.any(Integer.class));response.then().body("name", Matchers.is("Lisa"));
}

REST保證的PUT示例 (REST Assured PUT Example)

@Test
public void put_test() {Response response = given().contentType(ContentType.JSON).accept(ContentType.JSON).body("{\"name\": \"Lisa Tamaki\",\"salary\": \"20000\"}").when().put(ROOT_URI + "/update/3");System.out.println("PUT Response\n" + response.asString());// testsresponse.then().body("id", Matchers.is(3));response.then().body("name", Matchers.is("Lisa Tamaki"));response.then().body("salary", Matchers.is("20000"));
}

REST保證的DELETE示例 (REST Assured DELETE Example)

@Test
public void delete_test() {Response response = delete(ROOT_URI + "/delete/3");System.out.println(response.asString());System.out.println(response.getStatusCode());// check if id=3 is deletedresponse = get(ROOT_URI + "/list");System.out.println(response.asString());response.then().body("id", Matchers.not(3));
}

REST保證的XML REST Web服務示例 (REST Assured XML REST Web Services Example)

Below images show the output from our Jersey REST web service.

rest和restful的區別? 下圖顯示了我們的Jersey REST Web服務的輸出。

Success Case – Response Code 200

成功案例–響應碼200

Error Case – Response Code 500

REST Assured XML Error Response Example

錯誤案例-響應碼500

reshade使用教程,Here is our test class showing how to test both the cases using REST Assured.

這是我們的測試類,顯示了如何使用REST Assured測試這兩種情況。

package com.journaldev.restassured;import static io.restassured.RestAssured.given;import org.hamcrest.Matchers;
import org.testng.Assert;
import org.testng.annotations.Test;import io.restassured.http.ContentType;
import io.restassured.response.Response;public class RESTAssuredXMLTests {@Testpublic void post_xml_test() {Response response = given().contentType(ContentType.XML).accept(ContentType.XML).body("<empRequest>\n" + "	<id>1</id>\n" + "	<name>PK</name>\n" + "</empRequest>").when().post("https://localhost:8080/My-Jersey-Project/rest/emp/getEmp");System.out.println("POST Response\n" + response.asString());// testsAssert.assertEquals(response.getStatusCode(),200);response.then().body("empResponse.id", Matchers.is("1"));response.then().body("empResponse.name", Matchers.is("PK"));}@Testpublic void post_xml_error_test() {Response response = given().contentType(ContentType.XML).accept(ContentType.XML).body("<empRequest>\n" + "	<id>2</id>\n" + "	<name>PK</name>\n" + "</empRequest>").when().post("https://localhost:8080/My-Jersey-Project/rest/emp/getEmp");System.out.println("POST Error Response\n" + response.asString());// testsresponse.then().body("errorResponse.errorId", Matchers.is("2"));response.then().body("errorResponse.errorCode", Matchers.is("Wrong ID"));Assert.assertEquals(response.getStatusCode(),500);}}

We get the following output when above test class is executed.

當執行上述測試類時,我們得到以下輸出。

POST Error Response
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><errorResponse><errorCode>Wrong ID</errorCode><errorId>2</errorId></errorResponse>
POST Response
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><empResponse><id>1</id><name>PK</name></empResponse>
PASSED: post_xml_error_test
PASSED: post_xml_test

Notice that the major change is where we are setting the request body, there is no change in the way we are retrieving response elements, whether it’s JSON or XML response. This makes REST Assured powerful and easy to learn and use.

REST模式, 注意,主要的變化是我們設置請求主體的位置,檢索響應元素的方式沒有任何變化,無論是JSON還是XML響應。 這使得REST安全確保功能強大且易于學習和使用。

摘要 (Summary)

REST Assured helps us in testing our REST APIs easily. It integrates seamlessly with TestNG and JUnit. JSON Path and XML Path allows us to easily parse the response data and test specific elements. Since it uses Hamcrest API, there are many options to match actual result with expected data.

REST Assured幫助我們輕松測試REST API。 它與TestNG和JUnit無縫集成。 JSON Path和XML Path使我們能夠輕松解析響應數據并測試特定元素。 由于它使用Hamcrest API,因此有許多選項可將實際結果與預期數據進行匹配。

GitHub Repository.GitHub Repository下載完整的示例項目。

Reference: Official Documentation

參考: 官方文檔

rest的用法總結?翻譯自: https://www.journaldev.com/21501/rest-assured-tutorial

rest教程

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/182954.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息