Interface test 02-Usage of Postman

1. Postman assertion

Postman asserts using js language to write code to automatically determine whether the expected results are consistent with the actual results.
The assertion code is written in the Tests tag.

1.1 Assertion response status code

Status code: Code is 200
1) In the Tests tab, select Status Code: code is 200, and generate the corresponding code.
2) Appropriately adjust the test() method parameter 1 and the expected result in the anonymous function.
3) Click the send button to send the request and execute the assertion code.
4) View the assertion results.

// 断言响应状态码 是否为 200
pm.test("Status code is 200", function () {
    
    
    pm.response.to.have.status(200);
});

pm:代表 postman 的一个实例
test():是 pm实例的一个方法。有两个参数
    参数1:在断言成功后,给出的文字提示。可以修改。"Status code is 200"
    参数2:匿名函数。
pm.response.to.have.status(200); 
// 意思:postman 的响应结果中应该包含状态码 200
        200 ——> 预期结果!

1.2 Assert whether the response body contains a certain string

Response body: Contains string

// 断言响应体包含指定字符串
pm.test("Body matches string", function () {
    
    
  pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});

pm:postman的一个实例
test(): postman实例的方法,有两个参数
    参1:断言后显示的文字提示信息,可改。
    参2:匿名函数
pm.expect(pm.response.text()).to.include("string_you_want_to_search"); 
// 意思:pm 期望 响应文本 中,包含 xxxx 字符串。
    "string_you_want_to_search" ——> 预期结果。 可以修改

1.3 Assert whether the response body is equal to a certain string (object)

Response body: Is equal to a string

// 断言 响应体 等于某个字符串(对象)
pm.test("Body is correct", function () {
    
    
    pm.response.to.have.body("response_body_string");
});

pm.response.to.have.body("response_body_string");

// 意思是,pm 的 响应中应该有 响应体 xxx
"response_body_string" ——> 预期结果。 可以修改

1.4 Assert JSON data

Response body: JSON value check

// 断言json的响应结果
pm.test("Your test name", function () {
    
    
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
// var jsonData: 用js语法定义一个变量。jsonData 就是变量名
// pm.response.json();  代表响应的json结果

/* 举例:response.json();
{
    "success": true,
    "code": 10000,
    "message": "操作成功!",
    "data": "95c78d75-721c-40fb-b2d5-742fea42cbd5"
}
*/
pm.expect(jsonData.value).to.eql(100);
// 意思:pm 预期 json结果 key对应的值 等于 xxx
// to.eql(100); 中的 100 代表预期结果。可以修改的。

示例:
// 断言json的响应结果-success的值为true
pm.test("断言响应结果success的值为true", function () {
    
    
    var jsonData = pm.response.json();
    pm.expect(jsonData.success).to.eql(true);
});

pm.test("断言响应结果中code的值为10000", function () {
    
    
    var jsonData = pm.response.json();
    pm.expect(jsonData.code).to.eql(10000);
});

pm.test("断言响应结果中message的值为 操作成功", function () {
    
    
    var jsonData = pm.response.json();
    pm.expect(jsonData.message).to.eql("操作成功!");
});

1.5 Assertion response header

Response headers: Content-Type header check

// 断言响应头
pm.test("Content-Type is present", function () {
    
    
    pm.response.to.have.header("Content-Type");
});
// pm 的响应 头中包含 Content-Type

// 示例:可以在 header 中,添加 响应头中的 key 对应的 value 判定。用 ,隔分。
// 断言响应头
pm.test("Content-Type is present", function () {
    
    
    pm.response.to.have.header("Content-Type", "application/json;charset=UTF-8");
});

2. Global variables and environment variables

2.1 Global variables

Concept: Variables that are globally effective in Postman are globally unique

2.1.1 Settings

Code settings:pm.globals.set(“全局变量名”,全局变量的值)

pm.globals.set("glb_age", 100)

2.1.2 Obtain

Code acquisition :var 接收值的变量 = pm.globals.get(“全局变量名”)

var ret = pm.globals.get("glb_age")  // ret 的值为 100

Request parameter acquisition : (obtained through postman interface):{ {全局变量名}}

2.2 Environment variables

Concept: In a specific environment (production environment, development environment, test environment), the variables that take effect are unique within this environment.

特定环境
生产环境
测试环境
开发环境

2.2.1 Settings

Code settings:pm.environment.set("环境变量名", 环境变量值)

pm.environment.set("env_age", 99)

2.2.2 Get

Code acquisition:var 接收值的变量 = pm.environment.get("环境变量名")

var ret = pm.environment.get("env_age")  // ret 的值为 99

Request parameter acquisition (obtained through postman interface):{ {环境变量名}}

3. Postman request pre-script

After the send button is clicked, the request pre-script code is executed immediately. before the actual http request inside postman.
Case:
Call the Baidu homepage interface and send the timestamp to the server.
Implementation steps:
1) In the Pre-request Script tab, add code. Get the timestamp and write it to the global variable

// 拿到时间戳
var timestamp = new Date().getTime()
// 将时间戳设置到 全局变量
pm.globals.set("glb_timestamp", timestamp)

2) Click the Send button to send the request. The above code is executed before the request is sent. Write to global variables
3) View the written variables
View environment variables

4) In the request parameters (interface), use global variables. { {global variable name}}
Use global variables

5) In the postman console, view the http request sent
postman console
postman console

4. Postman association

Applied to when there is data association or dependency between multiple http requests.
Implementation steps :
Interface A depends on the data of interface B
1) Send an http request to interface B to obtain data
2) Set the data to global variables (environment variables)
3) Interface A obtains the data value in global variables (environment variables) and perform use.
Case:
Request to obtain the weather interface, extract the city in the response result, and use the city name to the Baidu search interface.
1) Create a use case set and create weather query requests and Baidu search requests respectively.

2) In the weather query request Tests, write code to obtain the city name and write it into the global variable

// 获取 全部响应结果
var jsonData = pm.response.json()
// 从响应结果中 获取 城市名
var city = jsonData.weatherinfo.city
// 将城市名写入到全局变量
pm.globals.set("glb_city", city)

3) Click the Send button to send the request and view the set global variables. (You can create global variables or local variables first, so you don’t need to click Send Request first. You can just execute the use cases in batches)

4) Modify Baidu search request, use global variables, and search by city name.
http://www.baidu.com/s?wd={ {glb_city}}

5. Execute test cases in batches


Batch execution use case set

6. Postman parameterization

6.1 Data files

  • Advantages of CSV
    :
    The data organization form is simple and suitable for situations with large amounts of data.
    Disadvantages:
    1) Bool type data is not supported. (After the data is read in by postman, "" is automatically added to wrap the bool value.)
    2) Interface testing with multiple parameters, few parameters, no parameters, and wrong parameters is not supported.
    3) Complex data types are not supported. (such as nested dictionaries, lists, etc.)
  • JSON :
    Advantages:
    1) Support bool type.
    2) Supports multiple parameters, few parameters, no parameters, and wrong parameters.
    3) Support complex data types.
    Disadvantages:
    For the same amount of data, the json data file size is much larger than the CSV file.

6.2 Import external data files

  • CSV file
    1) Create xxx.csv file. You can create a notepad file first and convert it to a csv file after writing.
    2) Write the data into the csv file.
    The first line written is the "field name" corresponding to the data.
    From the second line backward are the corresponding values, separated by commas.
    CSV file

3) In Postman, select the use case set that uses the data file and import the data file.
  a. Click the use case set name and use the Run button to enter the "Runner" page.
  b. Use the “Select File” button to select the xxx.csv file.
  c. Click the preview button to verify whether the data file is correct.
Import external files
Preview imported data

  • JSON file
    1) Create xxx.json data file
    2) In the data file, write json data according to json syntax. Postman requires that for data files in json format, the outermost layer of data content must be []. All internal data is stored using {}.
    3) In Postman, select the use case set that uses the data file and import the data file.
      a. Click the use case set name and use the Run button to enter the "Runner" page.
      b. Use the “Select File” button to select the xxx.json file.
      c. Click the preview button to verify whether the data file is correct.
    Preview imported json file

6.3 Read data file data

There are two ways to obtain the data in the data file depending on the location of the data used.
The first : In the request parameters (request line, request header, request body), use the data in the data file to wrap the { {}}csv file field name or the key in the json file,
such as: { {username}}or { {password}}
the second : code (assertion, request prefix Script), to use the data in the data file, you need to use the keyword data provided by postman to point to the field name of the csv file or the key of the json file,
such as: data.usernameordata.password
Case:
Requirement: Query the location and operator of mobile phone numbers in batches, and verify the correctness of operator data.
Interface: http://cx.shouji.360.cn/phonearea.php?number=13012345678
Test data:
Mobile phone number: 13012345678 Operation Provider: China Unicom Mobile
phone number: 13800001111 Operator: China Mobile Mobile
phone number: 18966778899 Operator: Telecom

1) Test the correspondence between a single interface, parameters and response body data.

2) Write the data file and organize the data according to the syntax format corresponding to csv or json.

3) Replace two places in the above use case:
Attribution: in the code. Replace mobile phone number with data. field name
: in the query parameters. Replace with { {field name}}.

4) Select the use case set that uses the data file and enter the Runner page. Import the data file and run.

5) If there is an exception, use the postman console to check.
postman console

Summary: Use data files to achieve "data-driven". ——How many pieces of data there are, corresponding to how many http requests there are.

7. Postman test report

7.1 Install newman

You need to install node.js first
npm install -g newman
npm install -g newman-reporter-html

7.2 Use newman to generate test reports

1) Execute test case sets in batches. (Confirmed)
2) Export the use case set. (Get xxxx.json file)
Export use case set

3) Export environment variables
Export environment variables

3) Execute the command in the terminal and generate the test report.
Complete command
newman run xxxx.json -e 环境变量文件 -d 外部数据文件 -r html --reporter-html-export 测试报告名.html
Example
newman run 批量执行测试用例.postman_collection.json -r html --reporter-html-export 我的第一个测试报告.html

Guess you like

Origin blog.csdn.net/Naruto_22/article/details/124212192