BH TT Postman Примеры тестов Postman // 1. SNIPPETS POSTMAN // Status code is 200 pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); // Status code: Code name has string pm.test("Status code name has string", function () { pm.response.to.have.status("Created"); }); // Status code: Successful POST request pm.test("Successful POST request", function () { pm.expect(pm.response.code).to.be.oneOf([201,202]); }); // Response body: JSON value check pm.test("Your test name", function () { var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(100); // Вместо value можно брать любой ключ }); -------------------------- // Get an environment variable pm.environment.get("variable_key"); // Get a global variable pm.globals.get("variable_key"); // Get a variable pm.variables.get("variable_key"); // Set an environment variable pm.environment.set("variable_key", "variable_value"); // Set a global variable pm.globals.set("variable_key", "variable_value"); // Clear an environment variable pm.environment.unset("variable_key"); // Clear a global variable pm.globals.unset("variable_key"); // Send a request pm.sendRequest("https://postman-echo.com/get", function (err, response) { console.log(response.json()); }); // Status code: Code is 200 pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); // Response body: Contains string pm.test("Body matches string", function () { pm.expect(pm.response.text()).to.include("string_you_want_to_search"); }); // Response body: JSON value check pm.test("Your test name", function () { var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(100); }); // Response body: Is equal to a string pm.test("Body is correct", function () { pm.response.to.have.body("response_body_string"); }); // Response headers: Content-Type header check 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: Successful POST request pm.test("Successful POST request", function () { pm.expect(pm.response.code).to.be.oneOf([201,202]); }); // Status code: Code name has string pm.test("Status code name has string", function () { pm.response.to.have.status("Created"); }); // Response body: Convert XML body to a JSON Object var jsonObject = xml2Json(responseBody); // Use Tiny Validator for JSON data // Validate response structure 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; }); |