test examples/test scripts
2018-06-01 15:33 改改~_~ 阅读(551) 评论(0) 编辑 收藏 举报1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | //https://www.getpostman.com/docs/v6/postman/scripts/test_examples //Setting an environment variable pm.environment.set( "variable_key" , "variable_value" ); //Setting a nested object as an environment variable 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)); //Getting an environment variable pm.environment.get( "variable_key" ); //Getting an environment variable (whose value is a stringified object) //-- 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" )); --------------------------------------------------- //Clear an environment variable pm.environment.unset( "variable_key" ); //Set a global variable pm.globals.set( "variable_key" , "variable_value" ); //Get a global variable pm.globals.get( "variable_key" ); //Clear a global variable pm.globals.unset( "variable_key" ); //Get a variable //This function searches for the variable across globals and the active environment. pm.variables.get( "variable_key" ); //Check if response body contains a string pm.test( "Body matches string" , function () { pm.expect(pm.response.text()).to.include( "string_you_want_to_search" ); }); //Check if response body is equal to a string pm.test( "Body is correct" , function () { pm.response.to.have.body( "response_body_string" ); }); //Check for a JSON value pm.test( "Your test name" , function () { var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(100); }); //Content-Type is present pm.test( "Content-Type is present" , function () { pm.response.to.have.header( "Content-Type" ); }); //Response time is less than 200ms pm.test( "Response time is less than 200ms" , function () { pm.expect(pm.response.responseTime).to.be.below(200); }); //Status code is 200 pm.test( "Status code is 200" , function () { pm.response.to.have.status(200); }); //Code name contains a string pm.test( "Status code name has string" , function () { pm.response.to.have.status( "Created" ); }); //Successful POST request status code pm.test( "Successful POST request" , function () { pm.expect(pm.response.code).to.be.oneOf([201,202]); }); //Use TinyValidator for JSON data 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 ; }); //Decode base64 encoded data 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 }); //Send an asynchronous request //This function is available as both a pre-request and test script. pm.sendRequest( "https://postman-echo.com/get" , function (err, response) { console.log(response.json()); }); //Convert XML body to a JSON object var jsonObject = xml2Json(responseBody); Sample data files //JSON files are composed of key/value pairs. Download JSON file For CSV files, the top row needs to contain variable names. //Download CSV file //Set the request to be executed next postman.setNextRequest( "request_name" ); //Stop workflow execution postman.setNextRequest( null ); postman.getResponseHeader(headerName) /*Test-only: returns the response header with name “headerName”, if it exists. Returns null if no such header exists. Note: According to W3C specifications, header names are case-insensitive. This method takes care of this. */ //will return the same value. postman.getResponseHeader( "Content-type" ) postman.getResponseHeader( "content-Type" ) |
pm.test("A single user was returned", function () { pm.expect(pm.response.json().results).to.have.lengthof(1); }); //Gender tests pm.test("Gender is male", function () { pm.expect(pm.response.json().result[0].gender).to.equal("male"); pm.expect(pm.response.json().result[0].gender).to.equal("mr"); }); //National test pm.test("The user is from the United States", function () { pm.expect(pm.response.json().result[0].nat).to.equal("US"); }); //---------------------------------------------------- //Here are some examples: // example using pm.response.to.have pm.test("response is ok", function () { pm.response.to.have.status(200); }); // example using pm.expect() pm.test("environment to be production", function () { pm.expect(pm.environment.get("env")).to.equal("production"); }); // example using response assertions pm.test("response should be okay to process", function () { pm.response.to.not.be.error; pm.response.to.have.jsonBody(""); pm.response.to.not.have.jsonBody("error"); }); // example using pm.response.to.be* pm.test("response must be valid and have a body", function () { // assert that the status code is 200 pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants // assert that the response has a valid JSON body pm.response.to.be.withBody; pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed });
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)