postman 笔记1:入门

2022-05-06  本文已影响0人  _百草_

1. Bulk Edit(x-www-form-urlencoded)

key-value edit样式,点击右侧Bulk Edit样式修改
bulk edit样式,点击Key-Value Edit样式修改

2. 控制台

postman中控制台位置
console.log("url2 = " + url2) # 控制台输出

3. pm

https://blog.csdn.net/qq_39314932/article/details/103037976

4. Pre-request Script 常用代码

4.1 pm.environment

4.2 pm.globals

全局变量
pm.globals.get("variable_key");
pm.globals.set("variable_key", "variable_value");
pm.globals.unset("variable_key");

4.3 pm.variables

变量
pm.variables.get("variable_key");
pm.environment.set("variable_key", "variable_value");
pm.environment.unset("variable_key");

4.4 pm.collectionVariables

collection变量
pm.collectionVariables.get("variable_key");
pm.collectionVariables.set("variable_key", "variable_value");
pm.collectionVariables.unset("variable_key");
注:collections

collections

5. Test 常用代码

基于nodejs,如JSON.parse

5.1 pm.test

pm.test函数定义测试,提供一个名称和函数,该函数返回一个布尔值(falsetrue),指示测试时通过还是失败

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);  //判断响应结果是否包含指定状态码
});
//
pm.response.to.have.status(code:Number):判断响应结果是否包含指定的状态码;
pm.response.to.have.status(reason:String):判断响应结果是否包含指定的状态描述;
pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});
状态码描述

5.2 pm.expect

通用断言,传递给expect函数的值是否符合预期;经常与pm.test结合使用

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
pm.test("Body matches string", function () {
  pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});
//
pm.response.to.have.body():判断响应结果是否包含消息体;
pm.response.to.have.body(optionalValue:String):判断响应结果是否等于指定的字符串;
pm.response.to.have.body(optionalValue:RegExp):判断响应结果是否包含指定的符合正则表达式规则的字符串;
pm.test("Your test name", function () {
    var jsonData = pm.response.json();  //获取返回body
    pm.expect(jsonData.value).to.eql(100);
});
var schema = {
    "items": {
        "type": "boolean"
    }
};

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

pm.test('Schema is valid', function () {
    pm.expect(tv4.validate(data1, schema)).to.be.true;
    pm.expect(tv4.validate(data2, schema)).to.be.true;
});
pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});
//
pm.response.to.have.header(key:String):判断响应结果是否包含指定的头部字段;
pm.response.to.have.header(key:String, optionalValue:String):判断响应结果是否包含指定的头部字段和值;
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
var jsonObject = xml2Json(responseBody);
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});
pm.except()接收一个断言内容
.to是连接符,如 bebeenisthatwhichandhashavewithatofsame
.include()用于指定断言方式和预期结果

5.3 pm.response

pm.response.to.not.be.error;
pm.response.to.have.jsonBody('');
pm.response.to.not.have.jsonBody('error');
pm.response.text()
pm.response.json();
pm.response.responseTime// 获取响应时间
pm.response.code //获取状态码

注:上述使用Postman for Windows(Version 8.11.1)
参考:

  1. Postman之Pre-request Script 使用详解
  2. Postman中Tests脚本总结
  3. Postman pm对象-测试相关功能
上一篇 下一篇

猜你喜欢

热点阅读