Rest-Assured 學習筆記

=·~·=發表於2024-10-20
Rest-Assured 學習筆記

Rest-Assured 學習筆記

安裝 Rest-Assured

<dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>4.3.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>
        

靜態引入方式

import static io.restassured.RestAssured.*;
        

GET 請求示例

package com.test.rest_asslured;
import static io.restassured.RestAssured.*;

public class ApiTest {
    public static void main(String[] args) {
        given().
                when().get("https://httpbin.org/get?name=zhangsan").
                then().log().all();
    }
}
        

使用 queryParam 的 GET 請求

package com.test.rest_asslured;
import static io.restassured.RestAssured.*;

public class ApiTest {
    public static void main(String[] args) {
        given().
                queryParam("name", "zhangsan").
                when().get("https://httpbin.org/get").
                then().log().all();
    }
}
        

引入 TestNG

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.7.1</version>
    <scope>test</scope>
</dependency>
        

Rest-Assured 語法

When-Then 是類似於驅動開發中定義的一種結構,Given 在某種場景下,When 發生什麼事情,Then 產生什麼結果。

  • given 設定測試預設(預設請求頭,請求引數,請求體,cookies等等)
  • when 所要執行的操作(GET, POST 等)
  • then 解析結果,斷言等。

POST 請求示例

@Test
public void ApiPostTest() {
    given().
            contentType("application/x-www-form-urlencoded;charset=UTF-8").
            formParam("name", "張三").
    when().
            post("https://httpbin.org/post").
    then().
            log().body();
}
        

使用 Jackson Databind

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.17.2</version>
</dependency>
        

斷言機制

TestNG 提供了基本的斷言機制,但通常使用 Hamcrest 斷言框架,它在 Rest-Assured 中已經整合。

import static org.hamcrest.Matcher.*;
        

JSON 引數示例

{
    "lotto": {
        "lottoId": 5,
        "winning-numbers": [2, 45, 34, 23, 7, 5, 3],
        "winners": [
            {
                "winnerId": 23,
                "numbers": [2, 45, 34, 23, 3, 5]
            },
            {
                "winnerId": 54,
                "numbers": [52, 3, 12, 11, 18, 22]
            }
        ]
    }
}
        

XML 引數示例

<shopping>
    <category type="groceries">
        <item>Chocolate</item>
        <item>Coffee</item>
    </category>
    <category type="supplies">
        <item>Paper</item>
        <item quantity="4">Pens</item>
    </category>
    <category type="present">
        <item when="Aug 10">Kathryn's Birthday</item>
    </category>
</shopping>
        

JSON 斷言解析示例

@Test
public void assertJson() {
    given().
    when().
            get("https://1857cad9-61db-44a6-b13f-6f518f76ba1d.mock.pstmn.io/json").
    then().
            // json斷言
             assertThat().body("lotto.winning-numbers.min()", equalTo(2));
}
        

響應體斷言 XML 示例

@Test
public void assertJsonXML() {
    given().
            when().
            get("https://1857cad9-61db-44a6-b13f-6f518f76ba1d.mock.pstmn.io/xml").
    then().
            // 斷言category節點第一個item的值為Paper
            assertThat().body("shopping.category.find{it.@type=='groceries'}.item", hasItems("Chocolate", "Coffee"));
}