关于软件测试:软件测试-接口测试中请求超时该怎么办

4次阅读

共计 1557 个字符,预计需要花费 4 分钟才能阅读完成。

​ A 发送申请,而后期待 B 的响应,同时开始超时计时,如果在超时工夫内胜利接管到响应,则完结期待和计时。如果到了超时工夫还没有接管到响应,则完结期待同时此次通信失败,这个过程叫做申请超时。在接口自动化测试过程中,也经常会碰到申请超时的场景。

如下图所示,测试用例 2 没有设置超时解决,遇到服务端阻塞,测试用例 2 始终处于期待的状态,前面的测试用例都不执行:

​编辑

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

​编辑

实战练习

​编辑

编写三条测试用例,在 test_two 测试用例中设置超时工夫为 3 秒,超过 3s 还没有失去响应的话则抛出异样,而后失常执行前面的测试用例。

Python 版本

Python 能够在调用申请办法时传入 timeout 参数管制超时工夫。

import requests
class TestReq:
def test_one(self):
r = requests.post(“https://httpbin.ceshiren.com/post”)
assert r.status_code == 200
def test_two(self):

通过 timeout 参数设置超时工夫,设置超时工夫为 0.1s,模仿超时场景

r = requests.post(“https://github.com/post”, timeout=0.1)
assert r.status_code == 200
def test_three(self):
r = requests.post(“https://httpbin.ceshiren.com/post”)
assert r.status_code == 200

Java 版本

Java 须要通过增加 RestAssured 的配置信息来解决超时的申请。通过 setParam() 设置超时工夫,第一个参数为连贯的类型,第二个参数为超时的最大时长,单位是 3000 毫秒。

import io.restassured.RestAssured;
import io.restassured.config.HttpClientConfig;
import io.restassured.config.RestAssuredConfig;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
public class ReqTimeoutTest
{
@Test
void timeout1(){
given().
when().get(“https://httpbin.ceshiren.com/get”).then().statusCode(200).log().all();
}
@Test
void timeout2(){
RestAssured.config=RestAssuredConfig.config().httpClient(HttpClientConfig.httpClientConfig().
setParam(“http.connection.timeout”,3000).
setParam(“http.socket.timeout”,3000).
setParam(“http.connection-manager.timeout”,3000));

given().when().get(“https://github.com/”).then().log().all();

}
@Test
void timeout3(){

given().when().get(“https://httpbin.ceshiren.com/get”).then().statusCode(200).log().all();
}
}

正文完
 0