Postman combat summary

Introduction to the

The actual combat content mainly includes the following points:

L Background

L Postman use, focus on automation, basic use will not be introduced

L Visual introduction to Newman

L Frame features

L Pit in actual combat

background

With the rapid development of domestic software technology, more and more manual testing is gradually replaced by automation, such as UI testing, interface testing, unit testing can be replaced by automation, domestic large Internet companies have long done automated testing is very mature, greatly improving the delivery efficiency and quality of products.

Under the general trend of IT industry, the product group faces the following four challenges:

1. The old underlying interface of Dubbo + Cloudt2. X needs to be upgraded.

2. The product basically realized bi-weekly iteration, and the regression test could not cover all modules. Occasionally, the correlation effect was not considered properly, resulting in the leak bug;

3. Insufficient testing resources, resulting in leakage of bugs, affecting product quality;

4. Tried to use Python, Doclever, Eolinker, Postman and other interface tests successively, but the effect was not ideal.

In this context, postman + Newman was finally established as an interface automation test scheme through research, combined with the experience of Using Postman in the product group, and the lack of automation ability of testers, postman was well packaged, the learning cost was low, and automated scripts could be produced quickly.

If you don’t want to experience the feeling of giving up after a few days when you can’t find the materials and no one answers the questions, you can follow me to discuss with you.

To recommend a software testing technology exchange group: 1079636098 group benefits free

Postman

Json use cases

The automated test case in Json**** format actually saves an array. The array elements are all objects, and an object corresponds to a use case, as shown in the following figure.

Use cases contain use case names (caseName), use case description (description), expect the call result (expectation), automated business types (requestType), input parameter (queryData), expected results (checkData) * * * *,Result check field (checkKey); You can flexibly set your own use case parameters according to the needs of the automation framework.

Postman**** provides several ways to access use case data. For later review, use cases in the following format are prepared:

[

{

“url”:”requestUrl”,

“params”:”params”

“body”:0,

“script”:{

“consoleInfo”:”helloWorld”

}

}

]

1. The interface address, which can access only the outermost properties of the use case object, is **{{variableName}}**

2. Params**, the use mode and rules are the same as the address **

3. Body**, use the same method and rules as address **

4. Pre-request-script & Tests**, where data points to the object of the use case. To obtain the attributes of the object, go to ****data.

The environment variable

Postman**** supports environment variables, which can be set to different environment variables, such as domain name, login account and password.

Environment variables can also be used in scripts as follows:

1. The interface address, params and body are used in the same way as the use case attributes are called, {{variableName}}. In case of variable conflicts, attributes in the use case are preferred, such as url=www.baidu.com in the environment variable. The use case object also has a URL =www.google.com property in its outermost property, so ****www.google.com is preferred

2. The use of environment variables in pre – and post-scripts varies. The postman wrapped method ** can be used as follows:

The global variable

PostmanSupport for setting global variables. As the name implies, global variables can be used in any environment, which limits them to setting some common variables. Interface address,paramsandbodyThe use of global variables and environment variables is not described here; Pre –/They are used in much the same way as environment variables in post-scripts, except thatPostman **** encapsulates method names differently:

** Get global variables: **pm.globals. Get (” variable_key “);

** Set global variables: **pm.globals. Set (” variable_key “, “variable_value”);

** Empty the environment variable: **pm.globals. Unset (” variable_key “);

In this automation exercise, the blogger abstracted common methods as global variables, such as obtaining time stamps, comparing data, and processingcookie, query data dictionary, etc.getTimeIs the method name, and on the right is the method body:

Common methods in global variables are used differently from variables and require special handling; If you want to use the get timestamp method in your script, you need toeval()Method references, as injavaIn theimport;

Note:

1. When using eval to reference a method, you do not need to add **() after the method name. When calling a method, you need to add ()****. 支那

Eval (environment.functionname); eval(environment.functionname);

3. Other methods can be called in this method, as shown in 1 and 2.

The script

The pre-script is executed before the interface is called. It generally pre-processes interface parameters, such as assigning values to input parameters. The post-script is executed after the interface is invoked and verifies the interface response. For environment variables, global variables,jsonExternal data access has been covered above and will not be described here.

These two parts arepostmanThe part that supports scripting that bloggers are used toJavaScriptLanguage writing; andpostmanListed some commonly used methods, here is no longer one of the introduction, mainly introduced a fewpostmanPoints not listed, but useful in post-scripting:

Postman.setnextrequest ()**** : jump

There are multiple interfaces in collection****, and a use case involves the invocation of multiple interfaces. If no jump statement is added, the next interface will be executed by default during automatic operation, making the execution process of the use case difficult to control. If jump statement is added, the jump will be flexible and the execution process will be more convenient.

End current use case execution:postman.setNextRequest(null);

Jump executes another interface:Postman. SetNextRequest (“The name of the interface“).;

2. Return ** : Returns **

The Tests script can be viewed as the body of a method, and the return in the figure above means that the method is returned and no further scripts are executed

3. ResponseCode ** : interface call status **

Generally you only need to use one of thesecode(Interface response code), using mode:responseCode.codeAnd the wholeresponseCodeAs follows:

{

“name”: “OK”,

“detail”: “Standard response for successful HTTP requests. The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request the response will contain an entity describing or containing the result of the action.”,

“code”: 200,

“standardName”: “OK”

}

4. ResponseBody ** : Response result **

Parse (responseBody); parse(responseBody); parse(responseBody);

5. Request ** : indicates the information about the current interface. The content is as follows: **

6. Tests [” Response assertion output “] = true: Response assertion is required in any script

The equal sign can be followed by a judgment statement. If true, the assertion passes; if false, the assertion fails. Such as judgment tests [” interface call succeeds, a status code of 200 “] = responseCode. Code = = 200, judge whether the interface call success.

Automated testing

usepostmanYou can do it individually locallycollectionRun automated tests,collection runnerWindow to execute automated scripts.

1. Environment **** Optional environment variables;

2. Iterations Set the number of execution examples. If the number of configured examples is less than the number in the JSON external file, only the configured examples are executed. If the number of use cases is more than the number of use cases in the JSON external file, the last use case is repeated after all use cases are executed until the set Iterations**** are completed.

3. Delay**** Set the execution interval.

4. Data You can select an external JSON use case. Note that an error will be reported if there is any problem with the JSON **** Data format in the file.

The running results are as follows:

Newman

Newman is a Postman nodeJS library that can be used to execute Postman’s JSON files on the command line. Newman makes it easy to run and test collections and use them to construct interfaces to automate testing and continuous integration.

Installation of Newman will not be described here; there are many installation tutorials available online.

Because Newman needs to use bat command to run, there are a large amount of automatic scripts, and writing command form is not conducive to management. To solve this problem, we developed Newman visual page, which can select environment variables and global variables, batch select scripts, automatically generate and execute BAT commands, improve efficiency and reduce the difficulty of using. The visualization page is shown below

Newman executed scripts that generated reports in three formats (XML, JSON, and HTML), which were poorly readable. The following studies were conducted to address this problem:

1. The JSON file is large and inefficient to read;

2. Make sure to start from the JS script that generates the report in Newman’s installation file (path: C:\Users\ Account \AppData\Roaming\ NPM \node_modules\ Newman \lib\reporters\json), reduce the size of the JSON report, only retain some useful content

3. Generate automatic reports in Excel format

4. Email sending: The nodeJS script automatically sends the processed report to the stakeholders in the form of email.

Characteristics of framework

Storage by service module

The actual combat is mainly for Web API, interface automation smoke test from the business dimension to split collection, not only test single interface anomaly, but also support the test between associated interfaces.

Data-driven mode

1. Automated use cases are all extracted to external JSON files;

2. There are different interfaces in the same collection.

3. In any interface, only one variable is stored in the use case for input or output parameters. After the maintenance of the interface, only the use case needs to be updated, and the script does not need to be modified. If input or output parameters need to be processed, they are processed in the pre-script. An example of this is shown below, where only one variable value is passed in body;

4. Expected results are saved in use cases. The same set of scripts applies to different service scenarios.

If you don’t want to experience the feeling of giving up after a few days when you can’t find the materials and no one answers the questions, you can follow me to discuss with you.

To recommend a software testing technology exchange group: 1079636098 group benefits free

Common method refining

In order to reduce the redundant scripts in automatic scripts and improve the writing efficiency of scripts, we extract common methods into global variables, which not only reduce the code complexity and improve the writing efficiency of scripts, but also reduce the difficulty of getting started with this automation framework. The use of public methods is described in the Postman introduction and will not be repeated here.

Multi-environment execution

On the basis of multiple environments, the primary keys of the underlying data are different. Faced with more and more automated scripts, the workload of maintenance is increasing.

In order to reduce this part of work, the data dictionary model is established through research:

1. Maintain the data dictionary in the environment into environment variables

2. Add variable identification to the part of the automation use case that needs to use the primary key of the underlying data

3. Write common methods to identify variables, match them in the data dictionary, and replace them with matching results.

Advantages: There is only one set of scripts and use cases, suitable for any environment, further reducing maintenance costs

Disadvantages: Reduced readability of use cases and inability to visualize data

Pit in action

1. If the variable used in body needs to be an object or array, the object or array needs to be converted into a string through json.stringify () and assigned to the variable to successfully call the interface;

Json external use case format, the body argument is passed to the bodyInfo property:

Pre-script processing:

Call from Body:

2. If a variable is defined in a pre – or post-script without an identifier, the defined variable can be used in the execution of other interfaces only after this use case is executed.

Variable defined in Query 1:

Query 2 output test contents:

The result is output after Collection Runner runs

3. The pre-script does not support interface jump: postman.setNextrequest ();

4. Collection can add pre-script, post-script and variable definition; Both pre-script and post-script are executed once without calling an interface.

I wish you and I meet, both gain! 1. Receive a 216-page software test engineer interview document for free. 2. Software test learning route and corresponding video learning tutorial free to share!