[Postman] Detailed explanation of advanced usage of Postman interface testing: assertion, global and environment variables, association, batch execution of use cases, reading external files to achieve parameterization

1. Postman assertion

1. Assertion position

  • Postman assertions are written in the JavaScript language and written in the [Tests] tag of Postman.
  • The script in [Tests] is executed after the request is sent, and it will finally display the assertion result (PASS/FAIL) in the [Test Results] tab.

2. Common assertions of Postman

  • Assert whether the response status code is 200 (Status code is 200)
  • Assert response body JSON data check (Response body: JSON value check)
  • Assert whether the response body contains the specified string (Response body: Contains string)
  • Assert whether the response body is equal to the specified string (Response body: Is equal to a string)
  • Assert whether the response header contains the specified header information (Response headers: Content-Type header
    check)

3. Operation example

insert image description here

2. Global variables and environment variables

1. Distinction between the two

Global variables: The scope of action is valid for all test sets under postman.
Environment variables: only take effect for the test set (development environment, test environment, production/online environment) that selects the corresponding environment. Variables in a set of environments cannot be repeated, but multiple non-repeated variables can be defined.

2. Set global variables

insert image description here
insert image description here
insert image description here

3. Set environment variables

insert image description here
insert image description here
insert image description here

3. Postman interface association

1. Concept

The request of the latter interface needs to rely on the response data of the previous interface. Generally, global variables or environment variables are used to transfer parameters between interfaces.

2. Operation steps

(1) Observe the response data of the front-end interface
. Assume that the returned json object is as follows:

{
    
    
  "tokeninfo":{
    
    
    "token":"xxxx",
    "xxx":"xxx"
  }
}

(2) Save the data to be passed in the global variable/environment variable in the front interface.
Write the code in the Test tag as follows:

var jsonData = pm.response.json()
var token = jsonData.tokeninfo.token

pm.globals.set("g_token",token);

(3) Directly use the saved global/environment variables in subsequent interfaces

4. Execute test cases in batches

1. Operation steps

insert image description here
insert image description here

2. View the results

insert image description here

5. Read external files to achieve parameterization

1. Usage scenarios

For a single interface, there is a large amount of data that needs to be tested in batches. We store the data in an external file, and then postman executes the script line by line by reading the external file.

2. Operation steps

(1) Prepare test data files

  • csv
    insert image description here
  • json
    insert image description here
    (2) set parameters
  • Used in the request, directly referenced by {{variable name}}
  • When used in an assertion, you need to use the built-in data method of postman, as shown in the figure below:
    insert image description here
    (3) Select the data file to execute in batches
    insert image description here

Guess you like

Origin blog.csdn.net/jylsrnzb/article/details/131926653