6

postman7种断言的使用 - Mrwhite86

 1 year ago
source link: https://www.cnblogs.com/mrwhite2020/p/16753710.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

postman7种断言的使用

导航:
1.postman断言介绍
2.状态码断言
3.响应正文断言-包含
4.响应正文断言-json字段
5.响应头断言
6.响应时间断言
7.环境变量的断言

- - - - - - - - - 分割线 - - - - - - - - - -
1.postman断言介绍
postman在发送请求后,需要对返回的结果做判断,验证是否符合预期,如状态码、响应头、响应正文等信息进行断言操作,在设置时会给定一个预期结果,在发送请求后会得到一个实际如果,如果两者结果一致,则认为是成功的(PASS),否则则是失败的(Fail),这种操作就叫做断言。断言主要是在Test操作框内进行设置,使用JavaScript语言编写,断言的结果在下方响应内容的TestResult内

1767782-20221004141501220-1014003747.png

2.状态码断言

说明: 对响应结果中的状态码进行验证,用于判断响应的状态码是否符合预期

(1)实例1:设置预期code=200,实际结果200(PASS)

tests["status code is 200"] = responseCode.code == 200;
1767782-20221004143154102-1785821374.png

(2)实例2:设置(2)实例2:预期code=404,实际结果200 (FAIL)

tests["status code is 404"] = responseCode.code == 404;
1767782-20221004143223746-717946116.png

 3.响应正文断言-包含

说明: 对响应结果中的响应正文进行验证,判断返回的响应正文是否包含指定的字符串(ps:大小写是敏感的)

实例1:设置返回包含helloworld,结果满足(PASS)

tests["Body matches"] =responseBody.has("helloworld");
1767782-20221004154035047-75556497.png

4.响应正文断言-json字段

说明: 对响应结果中的响应的json结构中的字段进行检查,看是否符合预期

(1)实例1:检查返回结果体 data - name 的json值是否为helloworld,符合预期(PASS)

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.data.name).to.eql("helloworld");
});

 

1767782-20221004154323438-225232613.png

(2)实例2:检查返回结果体 code的json值是否为200,不符合预期(FAIL)

pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.code).to.eql(200);
});
 
1767782-20221004154401909-1618959527.png

5.响应头断言

说明:对响应头部中的值(header)进行判断,是否包含对应的属性。

(1)实例1:检查header中是否包含Content-Encoding,符合预期(PASS)

pm.test("Content-Encoding is present", function () {
pm.response.to.have.header("Content-Encoding");
});
1767782-20221004154858802-1369436191.png

(2)实例2:检查header中是否包含Content-Type,不符合预期(FAIL)

pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
1767782-20221004154930698-852833636.png

6.响应时间断言

说明:验证响应结果返回的时间(responseTime)是否在指定范围,小于对应的时间(ms)则符合预期

(1)实例1:检测接口的响应时间小于200ms,符合预期(PASS)

pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
1767782-20221004155329061-1485871893.png

(2)实例2:检测接口的响应时间小于10ms,不符合预期(FAIL)

pm.test("Response time is less than 10ms", function () {
pm.expect(pm.response.responseTime).to.be.below(10);
});
1767782-20221004155412442-725572516.png

7.环境变量的断言

说明:使用这个断言的前提是postman中已经设置了环境变量,用于校验接口内的使用到的环境变量值的正确性

ps:环境变量在Pre-requestScript与Tests组件内都可以进行设置

(1)实例1:检测环境变量variable_key的值是否为hello,符合预期(PASS)

pm.environment.set("variable_key", "hello");//设置环境变量及变量值在Pre-requestScript
var data = pm.environment.get("variable_key");//获取环境变量
tests["检查环境变量值variable_key值是否为hello"] = data== "hello";
1767782-20221004160423263-796443530.png
1767782-20221004160255707-483557961.png

(2)实例2:检测环境变量time的值是否为2022-10-08,不符合预期(FAIL)

pm.environment.set("muber", 100);//设置环境变量及变量值
var number = pm.environment.get("muber");//获取环境变量
tests["检查环境变量值muber值是否为200"] = number== 200;
1767782-20221004160548894-12768431.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK