RestAssured API 测试演示
package com.example;
import io.restassured.RestAssured;
import io.restassured.config.LogConfig;
import io.restassured.filter.log.LogDetail;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
public class TestDemo {
@Test(description = "Verify that the Get Post API returns correctly")
public void verifyGetAPI() {
LogConfig logConfig = LogConfig.logConfig().enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.ALL).enablePrettyPrinting(true);
// Given
given()
.config(RestAssured.config().logConfig(logConfig))
.baseUri("https://jsonplaceholder.typicode.com")
.header("Content-Type", "application/json")
// When
.when()
.get("/posts/1")
// Then
.then()
.statusCode(200)
}
}
在测试方法中定义一个LogConfig配置,无论请求成功还是失败都会打印日志。
你可以试试
RestAssured
.given()
.log().everything(true)
.baseUri("https://jsonplaceholder.typicode.com")
.header("Content-Type", "application/json")
// When
.when()
.get("/posts/1")
// Then
.then()
.statusCode(200);