An interface test tools do use Postman (IV) - parameterized, execution test reporting cases (Concluded) ...

  introduction

  As already described postman assertions and parameter extraction, this is for the operation of the interface running, before running the interface, we need to do what the? There is no doubt that is parameterized, it can help us create test data in advance, for a number of encryption and authentication interface, we need to do this ahead of time, with these parameters to the request, the interface will pass. Let me talk about the parameters of actual testing, batch execution of test cases and test report generation.

 

 

  to parameterize

  Before introducing the " use Postman do interface testing tool (B) - environment variables and request parameters format " The article talked about setting environment variables, in fact, parameterization, and now, I introduced several other parametric forms.

The first: the parameters you define.

The following signature user interface as an example, the following code is the local development of the interface.

# 用户签名
# 鉴权: 签名+时间戳
def user_sign(request):
    if request.method == 'POST':
        client_time = request.POST.get('time','')
        client_sign = request.POST.get('sign','')
        address = request.POST.get('address','')
        print(client_sign,222)
        print(address,333)

    else:
        return '错误'

    if client_time == '' or client_sign == '':
        return '签名为空'

    # 服务器时间
    now_time = time.time()
    server_time = str(now_time).split('.')[0]
    # 时间差
    time_diff = int(server_time) - int(client_time)
    if time_diff >= 60:
        return '请求超时'

    # 签名
    md5 = hashlib.md5()
    sign_str = client_time + '&guest-test'
    # 转换成encode
    sign_bytes_utf8 = sign_str.encode(encoding = 'utf-8')
    md5.update(sign_bytes_utf8)
    server_sign = md5.hexdigest()

    if server_sign != client_sign:
        return '签名失败'
    else:
        return '签名成功'

 

  As can be seen from the above, the encryption method: client_time + '& guest-test'.

If the request is encrypted to the interface, the request must be in advance of the required parameters in the encrypted information parameterized, as follows:

 

Code:

//设置当前时间戳(10位)
postman.setGlobalVariable("time",Math.round(new Date()/1000));
time = postman.getGlobalVariable('time');

//设置KEY_WORD为全局变量
postman.setGlobalVariable("Key","&guest-test");
KEY_WORD = postman.getGlobalVariable("Key");

//字符串进行md5加密
var str = time+KEY_WORD;
var strmd5= CryptoJS.MD5(str).toString();
//signs设置成全局变量,signs
postman.setGlobalVariable("signs",strmd5);
//signss = pm.variables.get('signs')
console.log(time);
console.log(strmd5);

 

If you want to set 13 time stamp, you can write:

//设置当前时间戳(13位)
// postman.setGlobalVariable("time",Math.round(new Date().getTime()));
// time2 = postman.getGlobalVariable('time');

  

 Interface request before processing, are placed Pre-request Script.

Open the log to see whether it acquires value:

 

 

 

{} {} May use reference parameters, see results:

 

 

The second: Use postman comes with functions to parameterize.

In fact, this is the same as with the above, but use some built-in functions.

{{$ RandomInt}}: between 0 and 1000 to add a random integer

{{$ Timestamp}}: the current timestamp, accurate to the second, while the millisecond accuracy, increased back to 000

{{$ RandomDateRecent}}: Chinese generate a standard time in the format: Thu Mar 12 2020 08:15:08 GMT + 0800 (China Standard Time)

Use as follows:

 

 

Request to see the results of the background:

The time passed in randomDateRecent I do conversion, display formats:% Y-% m-% d% H:% M:% S

 

As can be seen, built-in functions rigid, unable to get random numbers you want, so we can write the script yourself.

Such as:

// 随机字符串
postman.setGlobalVariable("random_username", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
console.log(("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4))
// 0~9999的随机整数
postman.setGlobalVariable("random_number",Math.round(Math.random()*9999));
console.log(Math.round(Math.random()*9999))

 

 

Background check results:

 

id is a random integer from 0 to 9999, name is a random string of 4 bits.

 

Third: Use file parameterized, postman like jmeter all supporting documentation parameterized currently supported file formats: Json documents, txt file, CSV documents.

Prerequisites: In this paper, txt file, for example, create test data in txt inside, into eid and name two fields. Among the plurality of data separated by commas (comma in English)

After uploading files to see if the same:

 

 

Reference parameters and variables eid name

 

 

 

 

 

 

 

 

Run Parameters:

  • Environment: choose to run the environment, the environment is mainly determined environment variables;
  • Iterations: number of patients with iterations, that is currently selected these requests need to run a few times;
  • Delay: delay, to set the operating time between each request (in millimeters), if set, then a request to run after the appropriate waiting time to be run next request;
  • Log Responses: recording log response, which is a limiting set, the default is to record all log request, or may be restricted to only log errors recorded or not;
  • Data: Select the data file, which is provided Postman data-driven approach, variable data used for the current Collection in the request. Csv file support and Json format;
  • Keep variable values: keep the value of the variable. If there Collection script to reset the value of the environment variable or global variable, valid only when the time is running by default. If this option is checked, then the variable in the script to reset the value preserved, that is, directly modify Postman preset value of the variable;
  • Run collection without using stored cookies: If this option is selected, the time is not running Collection Postman use the cookie manager;
  • Save cookies after collection run: run, run during storage of cookies, this option is checked by default.

 

View Results:

 

 

 

   Run the test case

After selecting the collection, run the Run menu to the right

 

 

 

 

 

 View Results:

 

 

 

  Test reporting tools installed

  After doing interface testing, test results need to show the output in the form of reports, postman also provides reports, but the difference is you need to install the necessary tools --Newman.

Newman is based on a script can run the tool postman nodejs development, the use of Newman, and can be run directly from the command set of test postman.

  Installation nodejs

Download: https://nodejs.org/en/download/

 

 

 

 To see if the installation was successful, open cmd input node -v

 

 

 

 

  Installation newman

Note: newman nodejs must keep them in a file, it can be installed successfully, otherwise it will error of

 

 

  Html reporting module installation

 

 

First, the test case is derived from the postman

Create a folder, use cases guide out of the store directly to the folder you created them, to facilitate testing

 

 

 

 

 

 

  Manufacturing Test Report

Open cmd, store the path found use case. Execute the following commands

1- way the export of test cases:

newman run E:\postman_report\API文档.postman_collection.json

  

 

 

As can be seen from the figure before execution, iteration 1, 5 requests, failures 0.

Method 2 Test report generated html:

newman run E:\postman_report\API文档.postman_collection.json -r html

 

 If this occurs, indicating that the export of json test case does not support newman command, so again a guide.

 

 Execute the above command again:

 

 Enter E: after \ postman_report found no html report, after a series of struggles, his last suspected file name has caused Chinese and English changed, really successful.

Figure:

 

 

 

 If you do not specify the reports storage path, it will generate a default folder newman.

If you want to put a different directory, it can be:

E:\postman_report>newman run api.postman_collection.json --reporters html --reporter-html-export E:\postman_report

  

 

 

View the report:

 

 

This report is not too simple? Let's look pretty generate reports using other tools.

 

  Premium html report

Use htmlextra suite html generate more advanced reports
using the same methods and html, you need to install htmlextra, command line, type:

npm install -g newman-reporter-htmlextra

 

Generate reports using the command:

E:\postman_report>newman run api.postman_collection.json -r htmlextra --reporter-html-export

 

 

 

View the report:

 

 

 

Summary of the head through, Total Requests, Failed Tests, Skipped Tests view statistics, all requests by the failure cases, skipping like use cases.

  Below, all use cases:

 Click on each request can be viewed in detail, as shown below:

 

 

Failed use cases:

 

 

other:

Set Repeat times
Example: Repeat 10 times

newman run APITest.postman_collection.json -n 10

 

See other available parameters

newman run -h

 

 

 

postman interface test is basically a full content of these, of course, you can do and Jenkins continuous integration tool.

 

Small code word is not easy, please indicate. Welcome to the automated test development study and exchange groups: 696 400 122, concerned about the small series, full of dry goods for update. Learn and grow together ~

 

Published 82 original articles · won praise 43 · views 180 000 +

Guess you like

Origin blog.csdn.net/liudinglong1989/article/details/104872812