如何在 Rest Assured 中获取请求的响应时间?

rest assureddynamic programmingprogramming更新于 2025/4/5 11:52:17

我们可以在 Rest Assured 中获取请求的响应时间。请求发送到服务器后,收到响应所经过的时间称为响应时间。

响应时间默认以毫秒为单位。但是,我们也可以以其他时间单位获取。ResponseOptions 接口的以下方法可用于获取响应时间 −

  • getTime - 它以毫秒为单位获取响应时间。
  • getTimeIn(time unit) - 它以传递给此方法作为参数的时间单位获取响应时间。
  • time() - 它以毫秒为单位获取响应时间。
  • timeIn(time unit) - 它以传递给此方法作为参数的时间单位获取响应时间。

示例

Code Implementation

import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class NewTest {
   @Test
   public void responsetime() {
        //带有 Rest Assured 类的基本 URI
        RestAssured.baseURI ="https://www.tutorialspoint.com/index.htm";
        //输入详细信息
        RequestSpecification r = RestAssured.given();
        // GET 请求
        Response res = r.get();
        //以字符串形式获取 Response
        String j = res.asString();
        //获取响应时间
      	long c = res.getTime();
      	System.out.println("Response time in milliseconds: " + c);
   }
}

输出


相关文章