接口测试-Postman【3. 断言】

目录


在这里插入图片描述

1.断言

  • 通过使用断言,判断接口是否返回成功

1.1 断言函数

序号方法名解释
1Get an environment variable获取一个环境变量
2Get a global variable获取一个全局变量
3Get a variable获取一个变量【包括全局变量和环境变量】
4Get a collection variable获取一个集合变量
5Set an environment variable设置一个环境变量
6Set a global variable设置一个全局变量
7Set a collection variable设置一个集合变量
8Clear an environment varlable清空一个环境变量
9Clear a global variable清空一个全局变量
10Clear a collection varlable清空一个集合变量
11Send a request发送一个请求
12Status code: Code is 200检查状态码是否为200
13Response body: Contains string响应结果:字符串检查
14Response body: JSON value check响应结果:JSON检查
15Response body: Is equal to a string响应结果:等于指定字符串
16Response headers: Content-Type header check响应头属性检查
17Response time is less than 200ms响应时间是否小于200ms
18Status code: SuccessfulPOST requestPOST请求成功的状态码
19Status code: Code name has string检查状态码名字
20Response body: Converl XML body to a JSON Object将XML主题转化为JSON对象
21Use Tiny Validator for JSON data为JSON data使用微小验证器
22pm.response.code获取响应的状态码

1.2 具体使用

1.2.1 Get an environment variable【获取一个环境变量】

pm.environment.get("环境变量属性名");
var ip = pm.environment.get("ip");
console.log(ip)

1.2.2 Get a global variable【获取一个全局变量】

pm.globals.get("全局变量属性名");
var name = pm.globals.get("name");
console.log(name)

1.2.3 Get a variable【获取一个变量:包括全局变量和环境变量】

pm.variables.get("属性名");
var ip = pm.variables.get("ip");
var name = pm.variables.get("name");
console.log(ip)
console.log(name)

1.2.4 Get a collection variable【获取一个集合变量】

pm.collectionVariables.get("集合变量属性名");
var static = pm.collectionVariables.get("static");
console.log(static)

1.2.5 Set an environment variable【设置一个环境变量】

pm.environment.set("环境变量的key", "环境变量的value");
pm.environment.set("port", "5000");

1.2.6 Set a global variable【设置一个全局变量】

pm.globals.set("全局变量的key", "全局变量的value");
pm.globals.set("sex", "男");

1.2.7 Set a collection variable【设置一个集合变量】

pm.collectionVariables.set("集合变量的key", "集合变量的value");
pm.collectionVariables.set("password", "123456");

1.2.8 Clear an environment varlable【清空一个环境变量】

pm.environment.unset("环境变量key");
pm.environment.unset("port");

1.2.9 Clear a global variable【清空一个全局变量】

pm.globals.unset("全局变量key");
pm.globals.unset("sex");

1.2.10 Clear a collection varlable【清空一个集合变量】

pm.collectionVariables.unset("集合变量key");
pm.collectionVariables.unset("password");

1.2.11 Send a request【发送一个请求】

pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});
  • 发送GET方式的请求
pm.sendRequest("http://127.0.0.1:5000/demo/getRequest?name=张三&age=18", function (err, response) {
    console.log(response.json());
});
  • 发送POST方式的请求【请求内容为JSON对象】
const regRequest = {
    url: "http://"+pm.environment.get("ip")+":5000/demo/postRequest2",
    method: 'POST',
    header: 'Content-Type: application/json',  //注意要在Header中声明内容使用的类型
    body: {
        mode: 'raw',  // 使用raw(原始)格式
        raw: JSON.stringify({"name":"zhangsan","age":18}) //要将JSON对象转为文本发送
    }
};
pm.sendRequest(regRequest, function (err, response) {
    console.log(response.json());
});
  • 发送POST方式的请求【请求内容为XML】
//构造请求
const demoRequest = {
  url: 'http://httpbin.org/post',
  method: 'POST',
  header: 'Content-Type: application/xml',  // 请求头中指定内容格式
  body: {
    mode: 'raw',
    raw: '<xml>hello</xml>'  // 按文本格式发送xml
  }
};

//发送请求
pm.sendRequest(demoRequest, function (err, res) {
  console.log(err ? err : res.json());
});

1.2.12 Status code: Code is 200【检查状态码是否为200】

pm.test("Status code is 200", function () {
    pm.response.to.have.status(状态码);
});
pm.test("状态码是否为200", function () {
    pm.response.to.have.status(pm.response.code);
});

1.2.13 Response body: Contains string【响应结果:字符串检查】

pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("name");
});

1.2.14 Response body: JSON value check【响应结果:JSON检查】

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
pm.test("年龄是否为18", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.JsonData.age).to.eql(18);
});

1.2.15 Response body: Is equal to a string【响应结果:等于指定字符串】

pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});
pm.test("Body是否相等", function () {
    pm.response.to.have.body('{"Code":true,"Info":"Success","JsonData":{"age":"18","name":"张三"}}');
});

1.2.16 Response headers: Content-Type header check【响应头属性检查】

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});
pm.test("响应中是否包含Content-Type", function () {
    pm.response.to.have.header("Content-Type");
});

1.2.17 Response time is less than 200ms【响应时间是否小于200ms】

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(指定时间);
});
pm.test("响应时间是否低于200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

1.2.18 Status code: SuccessfulPOST request【POST请求成功的状态码】

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf(状态码列表);
});
pm.test("POST请求成功的状态码", function () {
	// 只要包含一个就PASS
    pm.expect(pm.response.code).to.be.oneOf([200, 201]);
});

1.2.19 Status code: Code name has string【检查状态码名字】

pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});
pm.test("状态返回值判断", function () {
    pm.response.to.have.status("OK");
});

1.2.20 Response body: Converl XML body to a JSON Objec【将XML主题转化为JSON对象】

var jsonObject = xml2Json(responseBody);

1.2.21 Use Tiny Validator for JSON data【为JSON data使用微小验证器】

var schema = {
    "items": {
        "type": "boolean"
    }
};

var data1 = [true, false];
var data2 = [true, 123];

pm.test('Schema is valid', function () {
	// 判断值的类型,只有都满足才PASS
    pm.expect(tv4.validate(data1, schema)).to.be.true;
    pm.expect(tv4.validate(data2, schema)).to.be.true;
});

3.如何创建集合变量

在集合之下创建的变量就是集合变量,集合变量的作用域只能在当前集合之下使用
在这里插入图片描述

4.批量运行测试用例

在这里插入图片描述

注意:如果需要上传文件,需要开启一下设置,并把文件拷贝到Postman工作路径下,Postman会在工作路径下找寻文件,还需要再上传的接口中选中文件
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Monly21

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值