postman语法

ads

  

小主 | 兰希姑娘


Postman 脚本断言书写存在新旧语法,建议使用新语法,本文介绍下旧语法长什么样子,新语法见往期文章。


总体来讲,旧语法,断言使用 tests 模块,每个tests 只能进行一个逻辑判定,比如:


tests["Body contains user_id"] = responsebody.has("user_id");tests["Body is correct"] = responseBody === "response_body_string";


新语法,断言使用pm.test,且一个test可以包含多个逻辑判定,比如:

 

const jsonData = pm.response.json();pm.test("Test data type of the response", () => {  pm.expect(jsonData).to.be.an("object");  pm.expect(jsonData.name).to.be.a("string");  pm.expect(jsonData.age).to.be.a("number");  pm.expect(jsonData.hobbies).to.be.an("array");  pm.expect(jsonData.website).to.be.undefined;  pm.expect(jsonData.email).to.be.null;});


最后附上Postman官网原文:


The older style of writing Postman tests relies on setting values for the tests object. Set a descriptive key for an element in the object and then assert if it's true or false. For example, the following will check if the response body contains the user_id string:

tests["Body contains user_id"] = responsebody.has("user_id");

Add as many keys as needed, depending on how many things you want to test for. View your test results in the response viewer under the Tests tab. The tab header shows how many tests passed, and the keys that you set in the tests variable are listed there. If the value evaluates to true, the test passed.

//set an environment variablepostman.setEnvironmentVariable("key", "value");
//set a nested object as an environment variableconst array = [1, 2, 3, 4];postman.setEnvironmentVariable("array", JSON.stringify(array, null, 2));const obj = { a: [1, 2, 3, 4], b: { c: 'val' } };postman.setEnvironmentVariable("obj", JSON.stringify(obj));
//get an environment variablepostman.getEnvironmentVariable("key");
//get an environment variable whose value is a stringified object//(wrap in a try-catch block if the data is coming from an unknown source)const array = JSON.parse(postman.getEnvironmentVariable("array"));const obj = JSON.parse(postman.getEnvironmentVariable("obj"));
//clear an environment variablepostman.clearEnvironmentVariable("key");
//set a global variablepostman.setGlobalVariable("key", "value");
//get a global variablepostman.getGlobalVariable("key");
//clear a global variablepostman.clearGlobalVariable("key");
//check if response body contains a stringtests["Body matches string"] = responseBody.has("string_you_want_to_search");
//check if response body is equal to a stringtests["Body is correct"] = responseBody === "response_body_string";
//check for a JSON valueconst data = JSON.parse(responseBody);tests["Your test name"] = data.value === 100;
//Content-Type is present (Case-insensitive checking)tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");//getResponseHeader() method returns the header value, if it exists
//Content-Type is present (Case-sensitive)tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
//response time is less than 200mstests["Response time is less than 200ms"] = responseTime < 200;
//response time is within a specific range//(lower bound inclusive, upper bound exclusive)tests["Response time is acceptable"] = _.inRange(responseTime, 100, 1001);
//status code is 200tests["Status code is 200"] = responseCode.code === 200;
//code name contains a stringtests["Status code name has string"] = responseCode.name.has("Created");
//successful POST request status codetests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;



技术精进是一场修行,相信自己和时间力量。


---  E N D ---

点“在看你懂得 

最后编辑于:2024/1/7 拔丝英语网

admin-avatar

英语作文代写、国外视频下载

高质量学习资料分享

admin@buzzrecipe.com