postman 笔记1:入门

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

  • 获取环境变量
    注:若无该变量则返回undefined
    pm.environment.get("variable_key"); # 注意变量前后的双引号
  • 设置环境变量
    pm.environment.set("variable_key", "variable_value");
  • 清除环境变量
    pm.environment.unset("variable_key");

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),指示测试时通过还是失败

  • 第一个参数,是字符串类型,表示该断言的内容
  • 第2个参数,回调函数。回调函数内部运行断言语句。
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):判断响应结果是否包含指定的符合正则表达式规则的字符串;
  • 使用json格式来判断值
pm.test("Your test name", function () {
    var jsonData = pm.response.json();  //获取返回body
    pm.expect(jsonData.value).to.eql(100);
});
  • 使用schema判断
    注:JsonSchema格式
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):判断响应结果是否包含指定的头部字段和值;
  • 响应时间小于200毫秒
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
  • 转换XML body为 JSON body
var jsonObject = xml2Json(responseBody);
  • 发送请求
    也可以放置在pre-requests中
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});
pm.except()接收一个断言内容
.to是连接符,如 bebeenisthatwhichandhashavewithatofsame
.include()用于指定断言方式和预期结果
  • .equal(value) 相等,即===
  • .not 取非,对之后的断言取反
  • .include(value)包含,value可以是Object|String|Number
  • .contains(value) 包含
  • .ok 判断是否为真;进行类型转换
  • .true 判断是否为真;不进行类型转换,只能是布尔值true

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对象-测试相关功能

你可能感兴趣的:(postman 笔记1:入门)