Table of Contents
- 1. Assertion
-
- 1.1 Assertion function
- 1.2 specific use
-
- 1.2.1 Get an environment variable【Get an environment variable】
- 1.2.2 Get a global variable【Get a global variable】
- 1.2.3 Get a variable【Get a variable: including global variables and environment variables】
- 1.2.4 Get a collection variable【Get a collection variable】
- 1.2.5 Set an environment variable【Set an environment variable】
- 1.2.6 Set a global variable【Set a global variable】
- 1.2.7 Set a collection variable【Set a collection variable】
- 1.2.8 Clear an environment varlable [clear an environment variable]
- 1.2.9 Clear a global variable [clear a global variable]
- 1.2.10 Clear a collection varlable [clear a collection variable]
- 1.2.11 Send a request [send a request]
- 1.2.12 Status code: Code is 200【Check whether the status code is 200】
- 1.2.13 Response body: Contains string [response result: string check]
- 1.2.14 Response body: JSON value check【Response result: JSON value check】
- 1.2.15 Response body: Is equal to a string [response result: equal to the specified string]
- 1.2.16 Response headers: Content-Type header check【Response header attribute check】
- 1.2.17 Response time is less than 200ms【Whether the response time is less than 200ms】
- 1.2.18 Status code: SuccessfulPOST request【Status code of successful POST request】
- 1.2.19 Status code: Code name has string【Check status code name】
- 1.2.20 Response body: Converl XML body to a JSON Objec【convert XML subject to JSON object】
- 1.2.21 Use Tiny Validator for JSON data【Use Tiny Validator for JSON data】
- 3. How to create collection variables
- 4. Run test cases in batches
1. Assertion
- By using assertions, determine whether the interface returns success
1.1 Assertion function
Number | Method name | Explanation |
---|---|---|
1 | Get an environment variable | Get an environment variable |
2 | Get a global variable | Get a global variable |
3 | Get a variable | Get a variable [including global variables And environment variables] |
4 | Get a collection variable | Get a collection variable |
5 | Set an environment variable | Set an environment variable |
6 | Set a global variable | Set a global variable |
7 | Set a collection variable | Set a Collection variables |
8 | Clear an environment varlable | Clear an environment variable |
9 | Clear a global variable | Clear a global variable |
10 | Clear a collection varlable | Empty a collection variable |
11 | Send a request | Send a request |
12 | Status code: Code is 200 | Check if the status code is 200 |
13 | Response body: Contains string | Response result: string check |
14 | Response body: JSON value check | Response result: JSON check |
15 | Response body: Is equal to a string | Response result: equal to the specified string |
16 | Response headers: Content-Type header check | Response header attribute check |
17 | Response time is less than 200ms | Response time is less than 200ms |
18 | Status code: SuccessfulPOST request | POST request successful status code |
19 | Status code: Code name has string | Check status code name |
20 | Response body: Converl XML body to a JSON Object | Convert XML body to JSON object |
21 | Use Tiny Validator for JSON data | Use Tiny Validator for JSON data |
22 | pm.response.code | Get the status of the response Code |
1.2 Specific use
1.2.1 Get an environment variable【Get an environment variable】
pm.environment.get("environment variable attribute name");
var ip = pm.environment.get("ip"); console.log(ip)
1.2.2 Get a global variable【Get a global variable】
pm.globals.get("global variable attribute name");
var name = pm.globals.get("name"); console. log(name)
1.2.3 Get a variable【Get a variable: including global variables and environment variables】
pm.variables.get("property name");
var ip = pm.variables.get("ip"); var name = pm. variables. get("name"); console.log(ip) console. log(name)
1.2.4 Get a collection variable【Get a collection variable】
pm.collectionVariables.get("Collection variable attribute name");
var static = pm.collectionVariables.get("static"); console. log(static)
1.2.5 Set an environment variable【Set an environment variable】
pm.environment.set("environment variable key", "environment variable value");
pm.environment.set("port", "5000");
1.2.6 Set a global variable【Set a global variable】
pm.globals.set("global variable key", "global variable value");
pm.globals.set("sex", "male");
1.2.7 Set a collection variable【Set a collection variable】
pm.collectionVariables.set("key of collection variable", "value of collection variable");
pm.collectionVariables.set("password", "123456");
1.2.8 Clear an environment varlable【Empty an environment variable】
pm.environment.unset("environment variable key");
pm.environment.unset("port");
1.2.9 Clear a global variable【Clear a global variable】
pm.globals.unset("global variable key");
pm.globals.unset("sex");
1.2.10 Clear a collection varlable【Clear a collection variable】
pm.collectionVariables.unset("collection variable key");
pm.collectionVariables.unset("password");
1.2.11 Send a request【Send a request】
pm.sendRequest("https://postman-echo.com/get", function (err, response) {<!-- --> console.log(response.json()); });
- Send a GET request
pm.sendRequest("http://127.0.0.1:5000/demo/getRequest?name=Zhang San & amp;age=18", function (err, response) {<!-- --> console.log(response.json()); });
- Send a POST request [the request content is a JSON object]
const regRequest = {<!-- --> url: "http://" + pm.environment.get("ip") + ":5000/demo/postRequest2", method: 'POST', header: 'Content-Type: application/json', //Note to declare the type of content used in the Header body: {<!-- --> mode: 'raw', // use raw (original) format raw: JSON.stringify({<!-- -->"name":"zhangsan","age":18}) //To convert the JSON object into text and send it } }; pm.sendRequest(regRequest, function (err, response) {<!-- --> console.log(response.json()); });
- Send a POST request [request content is XML]
//construct request const demoRequest = {<!-- --> url: 'http://httpbin.org/post', method: 'POST', header: 'Content-Type: application/xml', // specify the content format in the request header body: {<!-- --> mode: 'raw', raw: '<xml>hello</xml>' // send xml in text format } }; //send request pm.sendRequest(demoRequest, function (err, res) {<!-- --> console.log(err ? err : res.json()); });
1.2.12 Status code: Code is 200【Check whether the status code is 200】
pm.test("Status code is 200", function () {<!-- --> pm.response.to.have.status(status code); });
pm.test("Is the status code 200", function () {<!-- --> pm.response.to.have.status(pm.response.code); });
1.2.13 Response body: Contains string【Response result: string check】
pm.test("Body matches string", function () {<!-- --> pm.expect(pm.response.text()).to.include("string_you_want_to_search"); });
pm.test("Body matches string", function () {<!-- --> pm.expect(pm.response.text()).to.include("name"); });
1.2.14 Response body: JSON value check【Response result: JSON check】
pm.test("Your test name", function () {<!-- --> var jsonData = pm. response. json(); pm.expect(jsonData.value).to.eql(100); });
pm.test("Is the age 18", function () {<!-- --> var jsonData = pm. response. json(); pm.expect(jsonData.JsonData.age).to.eql(18); });
1.2.15 Response body: Is equal to a string【Response result: equal to the specified string】
pm.test("Body is correct", function () {<!-- --> pm.response.to.have.body("response_body_string"); });
pm.test("Body is equal", function () {<!-- --> pm.response.to.have.body('{"Code":true,"Info":"Success","JsonData":{"age":"18\ ","name":"Zhang San"}}'); });
1.2.16 Response headers: Content-Type header check【Response header attribute check】
pm.test("Content-Type is present", function () {<!-- --> pm.response.to.have.header("Content-Type"); });
pm.test("Does the response contain Content-Type", function () {<!-- --> pm.response.to.have.header("Content-Type"); });
1.2.17 Response time is less than 200ms【Whether the response time is less than 200ms】
pm.test("Response time is less than 200ms", function () {<!-- --> pm.expect(pm.response.responseTime).to.be.below(specified time); });
pm.test("Is the response time lower than 200ms", function () {<!-- --> pm.expect(pm.response.responseTime).to.be.below(200); });
1.2.18 Status code: SuccessfulPOST request【POST request successful status code】
pm.test("Successful POST request", function () {<!-- --> pm.expect(pm.response.code).to.be.oneOf(status code list); });
pm.test("POST request success status code", function () {<!-- --> // PASS as long as it contains one pm. expect(pm. response. code). to. be. oneOf([200, 201]); });
1.2.19 Status code: Code name has string【Check status code name】
pm.test("Status code name has string", function () {<!-- --> pm.response.to.have.status("Created"); });
pm.test("status return value judgment", function () {<!-- --> pm.response.to.have.status("OK"); });
1.2.20 Response body: Converl XML body to a JSON Object【Convert XML subject to JSON object】
var jsonObject = xml2Json(responseBody);
1.2.21 Use Tiny Validator for JSON data【Use Tiny Validator for JSON data】
var schema = {<!-- --> "items": {<!-- --> "type": "boolean" } }; var data1 = [true, false]; var data2 = [true, 123]; pm.test('Schema is valid', function () {<!-- --> // Judging the type of value, PASS only if all are satisfied pm.expect(tv4.validate(data1, schema)).to.be.true; pm.expect(tv4.validate(data2, schema)).to.be.true; });
3. How to create collection variables
The variable created under the collection is a collection variable, and the scope of the collection variable can only be used under the current collection
4. Run test cases in batches
Note: If you need to upload files, you need to open the settings, and copy the files to the Postman working path, Postman will look for the files in the working path, and you need to select the file in the upload interface