Skip to content

超时处理

超时处理

简介

在接口自动化测试过程中,时长会碰到请求超时的场景,A 发送请求,然后等待 B 的响应,同时开始超时计时,如果在超时时间内成功接收到响应,则结束等待和计时,宣告这次通讯成功;如果超过了超时时间还没有接收到响应,则结束等待和计时,宣告这次通讯失败,这个过程叫做请求超时。

使用场景

在网络延迟、服务器负载过高的情况下,对接口进行请求,那么可能会遇到请求超时报错,这时候就需要使用 timeout 参数控制请求时间,否则会导致用例堵塞。

如下图所示,测试用例2没有设置超时处理,遇到服务端阻塞,测试用例2一直处于等待的状态,后面的测试用例都不执行:

uml diagram

如下图所示,如果测试用例2设置了 3s 的超时时间,遇到服务端阻塞时,测试用例23s 之后则抛出异常,测试用例3正常执行:

uml diagram

实战演示

编写三条测试用例,在two测试用例中设置超时时间为 3 秒,超过 5s 还没有得到响应的话则抛出异常,后面的测试用例正常执行。

Python 演示代码

在 Python 中,可以通过调用请求方法时传入 timeout 参数控制超时时间。

import requests
class TestReq:

    def test_one(self):
        r = requests.post("https://httpbin.ceshiren.com/post")
        print(r)
        assert r.status_code == 200
    def test_two(self):
        try:
            # 通过timeout 参数设置超时时间,设置超时时间为0.1s,模拟超时场景
            r = requests.post("https://github.com/post", timeout=5)
            assert r.status_code == 200
        except requests.exceptions.Timeout:
            print("请求超时")
            return

    def test_three(self):

        r = requests.post("https://httpbin.ceshiren.com/post")
        print(r)
        assert r.status_code == 200

Java 演示代码:

import io.restassured.RestAssured;
import io.restassured.config.HttpClientConfig;
import io.restassured.config.RestAssuredConfig;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestReq {

    @BeforeAll
    static void setupClass() {
        RestAssured.baseURI = "https://httpbin.ceshiren.com";
    }

    @Test
    void testOne() {
        // 发送POST请求到 /post
        Response response = given()
                .when()
                .post("/post");

        // 打印响应
        System.out.println(response.asString());

        // 断言状态码为200
        assertEquals(200, response.getStatusCode());
    }

    @Test
    void testTwo() {
        // 自定义HttpClientConfig对象
        HttpClientConfig clientConfig = HttpClientConfig
                .httpClientConfig()
                .setParam("http.socket.timeout", 500)  // 设置响应超时时长为500毫秒
                .setParam("http.connection.timeout", 500); // 设置连接超时时长为500毫秒

        // 定义RestAssuredConfig对象
        RestAssuredConfig myTimeout = RestAssuredConfig
                .config()
                .httpClient(clientConfig);

        try {
            // 接口调用,使用自定义配置
            Response response = given()
                    .config(myTimeout)
                    .when()
                    .post("https://github.com/post"); // 注意: 这个URL可能会导致实际超时,确保在测试时使用有效URL

            // 断言状态码为200
            assertEquals(200, response.getStatusCode());
        } catch (Exception e) {
            // 处理请求超时异常
            System.out.println("请求超时");
        }
    }

    @Test
    void testThree() {
        // 发送POST请求到 /post
        Response response = given()
                .when()
                .post("/post");

        // 打印响应
        System.out.println(response.asString());

        // 断言状态码为200
        assertEquals(200, response.getStatusCode());
    }
}

总结

当执行到第二条用例超过超时时间还没有请求成功,会出现异常。第二条用例抛出异常后第三条用例正常执行。由此可见,遇到服务器阻塞的情况下,设置超时减少了等待时间并且不会影响后面测试用例的执行。