Postman-Tests模块测试方法记录
- 设置环境变量
1 | pm.environment.set( "variable_key" , "variable_value" ); |
- 将一个嵌套的对象设置为一个环境变量
1 2 3 4 5 6 7 | var array = [1, 2, 3, 4]; pm.environment.set( "array" , JSON.stringify(array, null , 2)); var obj = { a: [1, 2, 3, 4], b: { c: 'val' } }; pm.environment.set( "obj" , JSON.stringify(obj)) |
- 获得一个环境变量
1 | pm.environment.get( "variable_key" ); |
- 获得一个环境变量(其值是一个字符串化的对象)
1 2 3 4 5 | // These statements should be wrapped in a try-catch block if the data is coming from an unknown source. var array = JSON.parse(pm.environment.get( "array" )); var obj = JSON.parse(pm.environment.get( "obj" )); |
- 清除一个环境变量
1 | pm.environment.unset( "variable_key" ); |
- 设置一个全局变量
1 | pm.globals.set( "variable_key" , "variable_value" ); |
- 获取一个全局变量
1 | pm.globals.get( "variable_key" ); |
- 清除一个全局变量
1 | pm.globals.unset( "variable_key" ); |
- 获取一个变量(该函数在全局变量和活动环境中搜索变量)
1 | pm.variables.get( "variable_key" ); |
- 检查响应主体是否包含字符串
1 2 3 4 | pm.test( "Body matches string" , function () { pm.expect(pm.response.text()).to.include( "string_you_want_to_search" ); }); |
- 检查响应体是否等于字符串
1 2 3 | pm.test( "Body is correct" , function () { pm.response.to.have.body( "response_body_string" ); }); |
- 检查JSON值
1 2 3 4 | pm.test( "Your test name" , function () { var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(100); }); |
- Content-Type 存在
1 2 3 | pm.test( "Content-Type is present" , function () { pm.response.to.have.header( "Content-Type" ); });<em id= "__mceDel" style= "background-color: rgba(255, 255, 255, 1); font-family: "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px" > </em> |
- 返回时间少于200ms
1 2 3 4 | pm.test( "Response time is less than 200ms" , function () { pm.expect(pm.response.responseTime).to.be.below(200); }); |
- 状态码是200
1 2 3 4 | pm.test( "Status code is 200" , function () { pm.response.to.have.status(200); }); |
- 代码名包含一个字符串
1 2 3 4 | pm.test( "Status code name has string" , function () { pm.response.to.have.status( "Created" ); }); |
- 成功的POST请求状态码
1 2 3 4 | pm.test( "Successful POST request" , function () { pm.expect(pm.response.code).to.be.oneOf([201,202]); }); |
- 为JSON数据使用TinyValidator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 ; }); |
- 解码base64编码数据
1 2 3 4 5 6 7 8 9 10 11 12 13 | var intermediate, base64Content, // assume this has a base64 encoded value rawContent = 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-js pm.test( 'Contents are valid' , function () { pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be. true ; // a check for non-emptiness }); |
- 发送异步请求
此函数可作为预请求和测试脚本使用
1 2 3 4 | pm.sendRequest( "https://postman-echo.com/get" , function (err, response) { console.log(response.json()); }); |
- 将XML主体转换为JSON对象
1 | var jsonObject = xml2Json(responseBody); |
-----------------------------------------------------------------------------------------------------------------------------
1. 清除一个全局变量 Clear a global variable
对应脚本:
1 | postman.clearGlobalVariable( "variable_key" ); |
参数:需要清除的变量的key
2.清除一个环境变量 Clear an environment variable
对应脚本:
1 | postman.clearEnvironmentVariable( "variable_key" ); |
参数:需要清除的环境变量的key
3.response包含内容 Response body:Contains string
对应脚本:
1 | tests[ "Body matches string" ] =responseBody.has( "string_you_want_to_search" ); |
参数:预期内容
4.将xml格式的response转换成son格式 Response body:Convert XML body to a JSON Object
对应脚本:
1 | var jsonObject = xml2Json(responseBody); |
参数:(默认不需要设置参数,为接口的response)需要转换的xml
5.response等于预期内容 Response body:Is equal to a string
对应脚本:
1 | tests[ "Body is correct" ] = responseBody === "response_body_string" ; |
参数:预期response
6.json解析key的值进行校验 Response body:JSON value check
对应脚本:
1 | tests[ "Args key contains argument passed as url parameter" ] = 'test' in responseJSON.args |
参数:test替换被测的值,args替换被测的key
7.检查response的header信息是否有被测字段 Response headers:Content-Type header check
对应脚本:
1 | tests[ "Content-Type is present" ] = postman.getResponseHeader( "Content-Type" ); |
参数:预期header
8.响应时间判断 Response time is less than 200ms
对应脚本:
1 | tests[ "Response time is less than 200ms" ] = responseTime < 200; |
参数:响应时间
9.设置全局变量 Set an global variable
对应脚本:
1 | postman.setGlobalVariable( "variable_key" , "variable_value" ); |
参数:全局变量的键值
10.设置环境变量 Set an environment variable
对应脚本:
1 | postman.setEnvironmentVariable( "variable_key" , "variable_value" ); |
参数:环境变量的键值
11.判断状态码 Status code:Code is 200
对应脚本:
1 | tests[ "Status code is 200" ] = responseCode.code != 400; |
参数:状态码
12.检查code name 是否包含内容 Status code:Code name has string
对应脚本:
1 | tests[ "Status code name has string" ] = responseCode.name.has( "Created" ); |
参数:预期code name包含字符串
13.成功的post请求 Status code:Successful POST request
对应脚本:
1 | tests[ "Successful POST request" ] = responseCode.code === 201 || responseCode.code === 202; |
14.微小验证器 Use Tiny Validator for JSON data
对应脚本:
1 2 3 4 5 6 7 8 9 10 | 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); |
参数:可以修改items里面的键值对来对应验证json的参数
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步