目录
- 1.断言
- 1.1 断言函数
- 1.2 具体使用
- 1.2.1 Get an environment variable【获取一个环境变量】
- 1.2.2 Get a global variable【获取一个全局变量】
- 1.2.3 Get a variable【获取一个变量:包括全局变量和环境变量】
- 1.2.4 Get a collection variable【获取一个集合变量】
- 1.2.5 Set an environment variable【设置一个环境变量】
- 1.2.6 Set a global variable【设置一个全局变量】
- 1.2.7 Set a collection variable【设置一个集合变量】
- 1.2.8 Clear an environment varlable【清空一个环境变量】
- 1.2.9 Clear a global variable【清空一个全局变量】
- 1.2.10 Clear a collection varlable【清空一个集合变量】
- 1.2.11 Send a request【发送一个请求】
- 1.2.12 Status code: Code is 200【检查状态码是否为200】
- 1.2.13 Response body: Contains string【响应结果:字符串检查】
- 1.2.14 Response body: JSON value check【响应结果:JSON检查】
- 1.2.15 Response body: Is equal to a string【响应结果:等于指定字符串】
- 1.2.16 Response headers: Content-Type header check【响应头属性检查】
- 1.2.17 Response time is less than 200ms【响应时间是否小于200ms】
- 1.2.18 Status code: SuccessfulPOST request【POST请求成功的状态码】
- 1.2.19 Status code: Code name has string【检查状态码名字】
- 1.2.20 Response body: Converl XML body to a JSON Objec【将XML主题转化为JSON对象】
- 1.2.21 Use Tiny Validator for JSON data【为JSON data使用微小验证器】
- 3.如何创建集合变量
- 4.批量运行测试用例
1.断言
- 通过使用断言,判断接口是否返回成功
1.1 断言函数
序号 | 方法名 | 解释 |
---|---|---|
1 | Get an environment variable | 获取一个环境变量 |
2 | Get a global variable | 获取一个全局变量 |
3 | Get a variable | 获取一个变量【包括全局变量和环境变量】 |
4 | Get a collection variable | 获取一个集合变量 |
5 | Set an environment variable | 设置一个环境变量 |
6 | Set a global variable | 设置一个全局变量 |
7 | Set a collection variable | 设置一个集合变量 |
8 | Clear an environment varlable | 清空一个环境变量 |
9 | Clear a global variable | 清空一个全局变量 |
10 | Clear a collection varlable | 清空一个集合变量 |
11 | Send a request | 发送一个请求 |
12 | Status code: Code is 200 | 检查状态码是否为200 |
13 | Response body: Contains string | 响应结果:字符串检查 |
14 | Response body: JSON value check | 响应结果:JSON检查 |
15 | Response body: Is equal to a string | 响应结果:等于指定字符串 |
16 | Response headers: Content-Type header check | 响应头属性检查 |
17 | Response time is less than 200ms | 响应时间是否小于200ms |
18 | Status code: SuccessfulPOST request | POST请求成功的状态码 |
19 | Status code: Code name has string | 检查状态码名字 |
20 | Response body: Converl XML body to a JSON Object | 将XML主题转化为JSON对象 |
21 | Use Tiny Validator for JSON data | 为JSON data使用微小验证器 |
22 | pm.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会在工作路径下找寻文件,还需要再上传的接口中选中文件