REST Assured 21 - Response Time

REST Assured 系列汇总 之 REST Assured 21 - Response Time

客户端发起请求,服务器收到请求返回响应,从发请求到收到响应期间的时间称为Response Time响应时间。作为API测试,响应时间也是我们的测试点,API必须要快速响应。

REST Assured提供了便捷的方式去获取和断言响应时间。默认REST Assured获取响应时间以milliseconds 毫秒为单位,当然我们也可以按想要的时间单位获取时间。我们也可以断言响应时间比预期时的慢,快,或在期间。

如果你想获取响应时间以毫秒或其它时间单位,你可以用Response 接口中的time(), getTime(), timeIn(TimeUnit timeunit), getTimeIn( TimeUnit timeunit ) 方法,Response 接口继承自ResponseOptions接口。以上方法不能用Matchers 断言。

如果你想用Matchers,例如:断言响应时间大于等于某个值,可以使用ValidatableResponse接口中的重载方法time()ValidatableResponse接口是继承自alidatableResponseOptions接口。

是不是有些困惑?关于理解层级关系非常重要,否则你不知道用合适的类合适的方法,我们可以从实例中加深理解。

实例:

ResponseOptions接口
包含下面4个方法:

getTime() – 返回毫秒单位的响应时间(或 -1 如果没有响应时间可度量)
getTimeIn(TimeUnit timeunit) – 返回给定的时间单位的响应时间(或 -1 如果没有响应时间可度量)
time() – 返回毫秒单位的响应时间(或 -1 如果没有响应时间可度量)
timeIn(TimeUnit timeunit) – 返回给定的时间单位的响应时间(或 -1 如果没有响应时间可度量)

技术角度来说, getTime() 和 time() 是一样的, getTimeIn() 和 timeIn() 是一样的. 区别在于语法.

代码:

@Test
    public void mesaureResponseTimeUsingResponseOptionsMethods(){
        // There is no need to add escape character manually. Just paste string within double
        // quotes. It will automatically add escape sequence as required.
        String jsonString = "{\"username\" : \"admin\",\"password\" : \"password123\"}";

        // Create a request specification
        RequestSpecification request= RestAssured.given();

        // Setting content type to specify format in which request payload will be sent.
        // ContentType is an ENUM.
        request.contentType(ContentType.JSON);
        //Adding URI
        request.baseUri("https://restful-booker.herokuapp.com/auth");
        // Adding body as string
        request.body(jsonString);

        // Calling POST method on URI. After hitting we get Response
        Response response = request.post();

        // By default response time is given in milliseconds
        long responseTime1 = response.getTime();
        System.out.println("Response time in ms using getTime():"+responseTime1);

        // we can get response time in other format as well
        long responseTimeInSeconds = response.getTimeIn(TimeUnit.SECONDS);
        System.out.println("Response time in seconds using getTimeIn():"+responseTimeInSeconds);


        // Similar methods
        long responseTime2 = response.time();
        System.out.println("Response time in ms using time():"+responseTime2);

        long responseTimeInSeconds1 = response.timeIn(TimeUnit.SECONDS);
        System.out.println("Response time in seconds using timeIn():"+responseTimeInSeconds1);

    }

输出:

Response time in ms using getTime():3268
Response time in seconds using getTimeIn():3
Response time in ms using time():3268
Response time in seconds using timeIn():3

Process finished with exit code 0

ValidatableResponseOptions接口

这个接口有重载方法time(),可以接受Matcher

time(Matcher matcher) – 验证响应时间(毫秒为单位)是否匹配给定的期望.
time(Matcher macther, TimeUnit timeunit) – 验证响应时间(给定的时间单位)是否匹配给定的期望.

代码:

 @Test
    public void mesaureResponseTimeUsingValidatableResponseOptionsMethods() {

        // There is no need to add escape character manually. Just paste string within
        // double
        // quotes. It will automatically add escape sequence as required.
        String jsonString = "{\"username\" : \"admin\",\"password\" : \"password123\"}";

        // Create a request specification
        RequestSpecification request = RestAssured.given();

        // Setting content type to specify format in which request payload will be sent.
        // ContentType is an ENUM.
        request.contentType(ContentType.JSON);
        // Adding URI
        request.baseUri("https://restful-booker.herokuapp.com/auth");
        // Adding body as string
        request.body(jsonString);

        // Calling POST method on URI. After hitting we get Response
        Response response = request.post();

        // Getting ValidatableResponse type
        ValidatableResponse valRes = response.then();
        // Asserting response time is less than 2000 milliseconds
        // L just represent long. It is in millisecond by default.
        valRes.time(Matchers.lessThan(2000L));
        // Asserting response time is greater than 2000 milliseconds
        valRes.time(Matchers.greaterThan(2000L));
        // Asserting response time in between some values
        valRes.time(Matchers.both(Matchers.greaterThanOrEqualTo(2000L)).and(Matchers.lessThanOrEqualTo(1000L)));

        // If we want to assert in different time units
        valRes.time(Matchers.lessThan(2L), TimeUnit.SECONDS);
        
    }

完整代码:

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.concurrent.TimeUnit;


public class MeasuringResponseTimeInRestAssured {
    
    @Test
    public void mesaureResponseTimeUsingResponseOptionsMethods(){
        // There is no need to add escape character manually. Just paste string within double
        // quotes. It will automatically add escape sequence as required.
        String jsonString = "{\"username\" : \"admin\",\"password\" : \"password123\"}";

        // Create a request specification
        RequestSpecification request= RestAssured.given();

        // Setting content type to specify format in which request payload will be sent.
        // ContentType is an ENUM.
        request.contentType(ContentType.JSON);
        //Adding URI
        request.baseUri("https://restful-booker.herokuapp.com/auth");
        // Adding body as string
        request.body(jsonString);

        // Calling POST method on URI. After hitting we get Response
        Response response = request.post();

        // By default response time is given in milliseconds
        long responseTime1 = response.getTime();
        System.out.println("Response time in ms using getTime():"+responseTime1);

        // we can get response time in other format as well
        long responseTimeInSeconds = response.getTimeIn(TimeUnit.SECONDS);
        System.out.println("Response time in seconds using getTimeIn():"+responseTimeInSeconds);


        // Similar methods
        long responseTime2 = response.time();
        System.out.println("Response time in ms using time():"+responseTime2);

        long responseTimeInSeconds1 = response.timeIn(TimeUnit.SECONDS);
        System.out.println("Response time in seconds using timeIn():"+responseTimeInSeconds1);

    }

    @Test
    public void mesaureResponseTimeUsingValidatableResponseOptionsMethods() {

        // There is no need to add escape character manually. Just paste string within
        // double
        // quotes. It will automatically add escape sequence as required.
        String jsonString = "{\"username\" : \"admin\",\"password\" : \"password123\"}";

        // Create a request specification
        RequestSpecification request = RestAssured.given();

        // Setting content type to specify format in which request payload will be sent.
        // ContentType is an ENUM.
        request.contentType(ContentType.JSON);
        // Adding URI
        request.baseUri("https://restful-booker.herokuapp.com/auth");
        // Adding body as string
        request.body(jsonString);

        // Calling POST method on URI. After hitting we get Response
        Response response = request.post();

        // Getting ValidatableResponse type
        ValidatableResponse valRes = response.then();
        // Asserting response time is less than 2000 milliseconds
        // L just represent long. It is in millisecond by default.
        valRes.time(Matchers.lessThan(2000L));
        // Asserting response time is greater than 2000 milliseconds
        valRes.time(Matchers.greaterThan(2000L));
        // Asserting response time in between some values
        valRes.time(Matchers.both(Matchers.greaterThanOrEqualTo(2000L)).and(Matchers.lessThanOrEqualTo(1000L)));

        // If we want to assert in different time units
        valRes.time(Matchers.lessThan(2L), TimeUnit.SECONDS);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值