51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 930|回复: 0

学习postman tests测试框架在项目中最常用的实战运用

[复制链接]
  • TA的每日心情
    无聊
    2 小时前
  • 签到天数: 932 天

    连续签到: 4 天

    [LV.10]测试总司令

    发表于 2022-8-16 10:25:27 | 显示全部楼层 |阅读模式
    Postman常用方法集合:
    1.设置环境变量
    1.  postman.setEnvironmentVariable("key", "value");pm.environment.set("key", "value");//postman  5.0以上版本设置环境变量的方法
    复制代码
    2.设置全局变量
    1. postman.setGlobalVariable("key", "value");pm.globals.set("variable_key", "variable_value");//postman 5.0以上版本设置全局变量方法
    复制代码
    3.检查response body中是否包含某个string
    1. tests["Body matches string"] = responseBody.has("string_you_want_to_search");pm.test("Body is correct", function () {    pm.response.to.have.body("response_body_string");});//5.0以上版本方法
    复制代码
    4.检测JSON中的某个值是否等于预期的值
    1.  var data = JSON.parse(responseBody);tests["Your test name"] = data.value === 100;
    复制代码
    JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。

      如:检查json中某个数组元素的个数(这里检测programs的长度)
    1. var data = JSON.parse(responseBody);tests["program's lenght"] = data.programs.length === 5;
    复制代码
    5.转换XML body为JSON对象

    var jsonObject = xml2Json(responseBody);tests["Body is correct"] = responseBody === "response_body_string";

    6.检查response body是否与某个string相等


    7.测试response Headers中的某个元素是否存在(如:Content-Type)

     //getResponseHeader()方法会返回header的值,如果该值存在tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
     上面的方法,不区分大小写。下面的方法,要区分大小写。


    8.验证Status code的值
    tests["Status code is 200"] = responseCode.code === 200;pm.test("Status code is 200", function () {pm.response.to.have.status(200);});//5.0以上版本方法

    9.验证Response time是否小于某个值
    tests["Response time is less than 200ms"] = responseTime < 200;//5.0以上版本方法pm.test("Response time is less than 200ms", function () {    pm.expect(pm.response.responseTime).to.be.below(200);});

    10.name是否包含某个值
     tests["Status code name has string"] = responseCode.name.has("Created");//5.0以上版本方法pm.test("Status code name has string", function () {    pm.response.to.have.status("Created");});

    11.POST 请求的状态响应码是否是某个值
     tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;//5.0以上版本方法pm.test("Successful POST request", function () {    pm.expect(pm.response.code).to.be.oneOf([201,202]);});

    12.很小的JSON数据验证器
    var schema = {"items": {"type": "boolean"}};var data1 = [true, false];var data2 = [true, 123];console.log(tv4.error);tests["Valid Data1"] = tv4.validate(data1, schema);tests["Valid Data2"] = tv4.validate(data2, schema);

    13.获取request.data值:
    var Json = JSON.parse(request.data);
    data {object}:this is a dictionary of form data for the request. (request.data["key"]=="value")
      headers {object}:this is a dictionary of headers for the request (request.headers["key"]=="value")
      method {string}:GET/POST/PUT etc.
      url {string}:the url for the request.
      假设requestBody中有"version":"1.0";这个值,如果想获取到version的value值,代码如下:

    1.   var Json = JSON.parse(request.data); var version = Json["version"];
    复制代码

    14.JSON.parse()和JSON.stringify()
    1. JSON.parse()【从一个字符串中解析出json对象】JSON.stringify()【从一个对象中解析出字符串】var data={name:'goatling'}JSON.parse(data)结果是: '{"name":"goatling"}'JSON.stringify(data)结果是:name:"goatling"
    复制代码
    15.判断字段值是否为空typeof()

    1.  var Jsondata = JSON.parse(responseBody);if( typeof(Jsondata.data) != "undefined" )
    复制代码







    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-18 11:34 , Processed in 0.065832 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表