美文网首页
postman 断言

postman 断言

作者: 小娟_bb93 | 来源:发表于2018-08-29 22:49 被阅读0次

【Tests】例子

在发送请求并从服务器收到响应后运行测试脚本。

让我们看一下Postman-Tests模块的一些例子。其中大部分都是Postman内部的片段。您可以根据需要为请求提供尽可能多的测试。

设置环境变量

pm.environment.set("variable_key","variable_value");

将嵌套对象设置为环境变量

vararray=[1,2,3,4];pm.environment.set("array",JSON.stringify(array,null,2));varobj={a:[1,2,3,4],b:{c:'val'}};pm.environment.set("obj",JSON.stringify(obj));

获取环境变量

pm.environment.get("variable_key");

获取环境变量(其值是字符串化对象)

// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.vararray=JSON.parse(pm.environment.get("array"));varobj=JSON.parse(pm.environment.get("obj"));

清除环境变量

pm.environment.unset("variable_key");

设置全局变量

pm.globals.set("variable_key","variable_value");

获取全局变量

pm.globals.get("variable_key");

清除全局变量

pm.globals.unset("variable_key");

获取变量

此函数在全局变量和活动环境中搜索变量。

pm.variables.get("variable_key");

检查响应主体是否包含字符串

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");});

检查JSON值

pm.test("Your test name",function(){varjsonData=pm.response.json();pm.expect(jsonData.value).to.eql(100);});

内容类型存在

pm.test("Content-Type is present",function(){pm.response.to.have.header("Content-Type");});

响应时间小于200毫秒

pm.test("Response time is less than 200ms",function(){pm.expect(pm.response.responseTime).to.be.below(200);});

状态代码是200

pm.test("Status code is 200",function(){pm.response.to.have.status(200);});

代码名称包含一个字符串

pm.test("Status code name has string",function(){pm.response.to.have.status("Created");});

成功的POST请求状态代码

pm.test("Successful POST request",function(){pm.expect(pm.response.code).to.be.oneOf([201,202]);});

使用TinyValidator获取JSON数据

varschema={"items":{"type":"boolean"}};vardata1=[true,false];vardata2=[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;});

解码base64编码数据

varintermediate,base64Content,// assume this has a base64 encoded valuerawContent=base64Content.slice('data:application/octet-stream;base64,'.length);intermediate=CryptoJS.enc.Base64.parse(base64content);// CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-jspm.test('Contents are valid',function(){pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true;// a check for non-emptiness});

发送异步请求

此功能既可用作预请求脚本,也可用作测试脚本。

pm.sendRequest("https://postman-echo.com/get",function(err,response){console.log(response.json());});

将XML主体转换为JSON对象

varjsonObject=xml2Json(responseBody);

示例数据文件

JSON文件由键/值对组成。

下载JSON文件

对于CSV文件,顶行需要包含变量名称。

下载CSV文件

较旧的写作邮差测试风格

较旧的Postman测试编写风格依赖于特殊tests对象的设置值。您可以为对象中的元素设置描述性键,然后说明它是真还是假。例如,tests["Body contains user_id"] = responsebody.has("user_id");将检查响应主体是否包含user_id字符串。

您可以根据需要添加任意数量的密钥,具体取决于您要测试的内容。在响应查看器下的“ 测试”选项卡下,您可以查看测试结果。选项卡标题显示传递了多少测试,并在此处列出了您在tests变量中设置的键。如果值的计算结果为true,则测试通过。

设置环境变量

postman.setEnvironmentVariable("key","value");

将嵌套对象设置为环境变量

vararray=[1,2,3,4];postman.setEnvironmentVariable("array",JSON.stringify(array,null,2));varobj={a:[1,2,3,4],b:{c:'val'}};postman.setEnvironmentVariable("obj",JSON.stringify(obj));

获取环境变量

postman.getEnvironmentVariable("key");

获取环境变量(其值是字符串化对象)

// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.vararray=JSON.parse(postman.getEnvironmentVariable("array"));varobj=JSON.parse(postman.getEnvironmentVariable("obj"));

清除环境变量

postman.clearEnvironmentVariable("key");

设置全局变量

postman.setGlobalVariable("key","value");

获取全局变量

postman.getGlobalVariable("key");

清除全局变量

postman.clearGlobalVariable("key");

检查响应主体是否包含字符串

tests["Body matches string"]=responseBody.has("string_you_want_to_search");

将XML主体转换为JSON对象

varjsonObject=xml2Json(responseBody);

检查响应主体是否等于字符串

tests["Body is correct"]=responseBody==="response_body_string";

检查JSON值

vardata=JSON.parse(responseBody);tests["Your test name"]=data.value===100;

存在Content-Type(不区分大小写的检查)

tests["Content-Type is present"]=postman.getResponseHeader("Content-Type");//Note: the getResponseHeader() method returns the header value, if it exists.

内容类型存在(区分大小写)

tests["Content-Type is present"]=responseHeaders.hasOwnProperty("Content-Type");

响应时间小于200毫秒

tests["Response time is less than 200ms"]=responseTime<200;

响应时间在特定范围内(包含下限,上限不包括)

tests["Response time is acceptable"]=_.inRange(responseTime,100,1001);// _ is the inbuilt Lodash v3.10.1 object, documented at https://lodash.com/docs/3.10.1

状态代码是200

tests["Status code is 200"]=responseCode.code===200;

代码名称包含一个字符串

tests["Status code name has string"]=responseCode.name.has("Created");

成功的POST请求状态代码

tests["Successful POST request"]=responseCode.code===201||responseCode.code===202;

使用TinyValidator获取JSON数据

varschema={"items":{"type":"boolean"}};vardata1=[true,false];vardata2=[true,123];tests["Valid Data1"]=tv4.validate(data1,schema);tests["Valid Data2"]=tv4.validate(data2,schema);console.log("Validation failed: ",tv4.error);

解码base64编码数据

varintermediate,base64Content,// assume this has a base64 encoded valuerawContent=base64Content.slice('data:application/octet-stream;base64,'.length);intermediate=CryptoJS.enc.Base64.parse(base64content);// CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-jstests["Contents are valid"]=CryptoJS.enc.Utf8.stringify(intermediate);// a check for non-emptiness

相关文章

  • 接口测试02-Postman的使用

    Postman 断言 postman 断言借助js 语言编写代码,自动判断预期结果与实际结果是否一致。 断言 代码...

  • postman使用心得(二):postman接口测试断言

    在使用postman做接口测试的时候,可以在tests中对该接口进行断言设置 上面的图为使用postman断言方法...

  • postman 断言

    【Tests】例子 在发送请求并从服务器收到响应后运行测试脚本。 让我们看一下Postman-Tests模块的一些...

  • Postman断言

    一般来说执行完测试,我们需要对测试结果进行校验,判断结果是否符合我们的预期,也就是断言。实例断言内容 响应状态码:...

  • postman 断言

    1.环境变量 (1)设置环境变量: postman.setEnvironmentVariable("key", "...

  • postman 断言

    1.什么是断言 判断程序执行结果是否符合我们的预期就是断言 2.接口测试根据什么判定断言 一般根据程序返回状态码和...

  • Postman 断言

    Postman断言填写在Tests中,通过JavaScript(JS)来实现的。所以要有一点JS的基础。响应断言结...

  • postman断言

    可以使用右边的样例,点击即可使用

  • postman断言记录

    postman 断言: 断言请求结果状态码:status code: Code is 200pm.test("St...

  • 如何使用Fiddler做接口测试?测试小白进阶大咖的必经之路!

    我们都知道使用Postman做接口测试,相比Postman,Fiddler不能写断言,只能构造HTTP请求,并人工...

网友评论

      本文标题:postman 断言

      本文链接:https://www.haomeiwen.com/subject/yddlwftx.html