网站首页 > 博客文章 正文
本文节选自霍格沃兹测试开发学社内部教材
获取更多相关资料点击:Redirecting...
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();
}
}
猜你喜欢
- 2024-10-31 英雄联盟手游错误代码100008什么原因?LOL手游错误代码解决办法
- 2024-10-31 玩转网络自动化之ntc_templates模块
- 2024-10-31 C#程序与单片机通信时,如何自动初始化串口(2)?
- 2024-10-31 电机驱动_上位机_ModbusRTU通讯(伺服上位机)
- 2024-10-31 真实的线上故障处理案例(在线故障检测时应注意什么)
- 2024-10-31 问题反馈:为什么Cydia加载出现那么多红色错误?
- 2024-10-31 Norland诺兰德连接超时连接报错?一招解决连接问题
- 2024-10-31 处理超时订单(超时未付款)的解决方案
- 2024-10-31 第一后裔运行超时运行报错怎么办?一招解决运行问题
- 2024-10-31 基于 PTS 压测轻松玩转问题诊断(tps压测 指什么)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)