Interface automation testing and further implementation of CI through Postman+Newman+Jenkins

Table of contents

Foreword:

Postman section

Collection or test set establishment

Detailed explanation of Postman usage

test tools

mock server

Installing and using Newman

use of newman

Support for SSL The third version of newman (currently 4.3.1) can support SSL through CLI options

Configure proxy for newman: configure the proxy by setting the request environment variable of Postman.

Jenkins part

Disadvantages:


Foreword:

It is a common solution to use Postman+Newman+Jenkins for interface automation testing and continuous integration (CI). Postman is a popular API development and testing tool, Newman is a command-line tool for Postman, and Jenkins is a popular continuous integration tool.

I believe that when it comes to interface testing, everyone knows and has used Postman, and thinks it is a very easy-to-use interface testing tool. In fact, Postman is very powerful, and there are many interesting and practical functions. It is for everyone to explore and share together. For example, whether Postman can perform automated interface testing is actually possible, and now everyone is constantly mentioning TDD and how to build a pipeline, and the demand for this is getting stronger and stronger.

To give a simple real scenario, the development provides more than 300 APIs, each of which has various parameters, so we will first write 300*n testcases for these more than 300 APIs in postman, if we all use postman to run , In fact, the efficiency is not high. As an engineer, graphics are not as easy to use as commands, so you must use newman. Since you can use the command line to execute it, why can’t you use CI?

  • Just imagine, when the development has been maintaining these more than 300 interfaces, does the development need to tell us that the testers should test every time? As long as these interface tests are automatically executed through Jenkins, basically CI has completed a lot.

  • If you can monitor which interfaces have changed and automatically select the relevant collections, that is, test suits, it is another step forward on the road to a more powerful one.

  • Jenkins can also automatically send emails to the development and you, and can also automatically submit bugs. If this is the case, you will be more comfortable, just read the emails.

Postman section

This is too simple, just skip it, search on Baidu, download and install a native one, because it is no longer supported as a Chrome plug-in. Let's get straight to the point.

Collection or test set establishment

In Postman, a Collection is similar to a folder. You can put requests for the same project in a Collection for easy management and sharing, and you can also create folders in the Collection. If you want to make API documents, you can have one request for each API. If you want to test all kinds of inputs, you need one request for each test.

In Postman, the Collection export can be saved as a json file. With this file, the command line execution can be completed through the Postman CLI to realize automated testing.

  • Import: Used to import API request files saved by you or your team, in json format.

  • New folder: used to classify API requests for easy management.

  • Save request: Save your API request, and the return value can also be stored.

  • Download: Download the API request you have passed the test, share it with the team, and import it. json format, manually editable

Postman allows users to send any type of HTTP request, such as GET, POST, HEAD, PUT, DELETE, etc., and can allow arbitrary parameters and headers.

She supports different authentication mechanisms, including Basic Auth, Digest Auth, OAuth 1.0, OAuth 2.0, etc.

She can also respond to data automatically formatted according to syntax highlighting, including HTML, JSON and XML.

  • You can record test cases through "Capture API request with Postman". This method is also good, you can try it.

Detailed explanation of Postman usage

  • request request

  The postman interface is divided into two parts: the sidebar on the left and the request builder on the right: quickly create almost all HTTP requests: URL, request method, headers, body.

  • responses

Ensuring the correctness of API responses is most of the work you need to do. The response viewer part of postman will help you with this and make it easy.

  An API response contains body, headers, and response status code. postman puts body and headers in different tabs. Response codes and response times are displayed next to the tabs. Hover over the response code to see more detailed information.

  • write assertion tests

Postman's Tests tag can be used to write assertion tests and provides a large number of APIs, which is very convenient and practical.

  • Run Collections

Postman allows you to run a collection, which is actually what we call a test suit in the testing field, and can be configured to run any number of times. Finally, a result of the overall operation will be given. The results of each run are saved and provided for you to compare the differences fired between each run.

  Select collection, select environment. Click the Run button.

test tools

The test tool mainly includes three parts, the Pre-request that runs before the request is initiated, the Test that runs after receiving the response, and the Collection Runner that runs all requests at once

  • Pre-request

Both Pre-request and Test use JavaScript. Postman executes the code in a sandbox. The libraries and functions provided to users can be viewed here. Commonly used functions can be realized through the Code Snippets on the right, and can be inserted into the code area by clicking

You can see that there are two commonly used functions in Pre-request, setting environment variables and setting global variables. The pre-request of this request is to generate a string as a random username before registration.

postman.setEnvironmentVariable("random_username", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));

Other uses include getting the current timestamp and putting it in the parameter before making the request:

postman.setEnvironmentVariable("unixtime_now", Math.round(new Date().getTime()/1000));

Of course, it can also be used to generate a check string. In short, any things that need to be modified manually before sending the request can be automatically realized by scripting.

  • Test

Compared with Pre-request, Test's Snippets are much richer, such as checking status code, checking response string, validating JSON, checking header, and limiting response time.

If you need to save the data responded by the server and use it in subsequent requests, you also need to do it in this step.

In the Test in the figure, I first check that the status code is 200, then parse the returned JSON, and set the token in the environment variable to the token in the JSON.

  • Collection Runner

After writing a lot of tests, you can use the Collection Runner to automatically run the entire Collection. The entry is at the Runner on the top row of the main interface. Select Collection and Environment. If necessary, you can also load JSON and CSV as data sources. Click Start Test Run to see the result.

mock server

Postman's support for Mock server is also very important. Whether it is for front-end development or back-end testing, there are many interfaces between different servers, and everyone's development progress is different. In addition, environmental isolation can be achieved. Postman implements the interface protocol reached by the front-end/back-end/test by adding examples, so that everyone can divide the work and work in parallel. The original text is as follows.
 

This behavior allows teams to mock an example request and response, in addition to simulating the endpoint using mock servers. Front-end and back-end developers and testers can all begin working in parallel, based on the agreed-upon example.

Of course, there are many more mock frameworks. Here I recommend you a very convenient mock framework is moco, which has been open sourced on github.
There is also a mock server that is slightly more complicated, but it is also very easy to use.

Installing and using Newman

### Install via npm
If you want to install globally:

npm install -g newman 

use of newman

On the npm website, you can find newman's special page with very detailed instructions.

  • Support local json files as parameters to run

The json file can be exported from Postman, select a collection, you can easily export your Postman Collection and save it as a json file, and then put it in your local directory to run.

$ newman run examples/sample-collection.json

  • It also supports running via url as a parameter

If you already have a CI platform, you can put these Collections on a certain server, and newman cli also supports running through url.

$ newman run https:or http: your json url

  • Another way is to call newman as a module of Node.js, which is more suitable for large-scale projects, please refer to the official sample code.
 const newman = require('newman'); // require newman in your project

// call newman.run to pass `options` object and wait for callback

newman.run({

    collection: require('./sample-collection.json'),

    reporters: 'cli'

}, function (err) {

    if (err) { throw err; }

    console.log('collection run complete!');

});

  • If you need more commands or help, you can get them through the following commands.
 $newman -h, --help

$newman run -h

Support for SSL The third version of newman (currently 4.3.1) can support SSL through CLI options

--ssl-client-cert path_to_public_cert_file

--ssl-client-key path_to_private_client_key

--ssl-client-passphrase the_secret_passphrase

Configure proxy for newman: configure the proxy by setting the request environment variable of Postman.

HTTP_PROXY / http_proxy

HTTPS_PROXY / https_proxy

NO_PROXY / no_proxy

Complete the request settings of postman

Using newman's reporter function
newman integrates and supports the following reporting methods: cli, json, junit, progress and emojitrain.

CLI is supported by the default option, and other options need to be displayed and written later, for example:

$ newman run examples/sample-collection.json -r cli,json

Jenkins part

For the installation and configuration of Jenkins, you can refer to relevant documents. If the master-slave multi-level architecture is not configured, it is easy to build, so skip it here.

After completing the Jenkins setup, start creating a job to execute the newman command. Make sure your newman command is executable on the Jenkins server. For example, put the path in PATH and execute it first.

newman -v

  • Choose a freestyle job

  • You can choose to execute it regularly. For this part, you can search online how to execute a job regularly. Generally speaking, it is 5 lines of data, according to your needs. For example, if I want to execute it every 10 minutes, I just write "H/10 * * * *".

This part can also be triggered by monitoring code submission, I just give an example.

  • Add your shell command in the part of executing the shell, which is actually your newman command

The main line here is that your json file is placed there. As mentioned earlier, it can be placed on the internal server. Give a url. Newman also supports it, and it can also be placed on the Jenkins server.

  • Other email configurations or how to automatically submit bugs are relatively normal. Please break through by yourself. I believe that only by breaking through can you have a greater sense of gain.

Disadvantages:

There should not be too many API interfaces, not more than 300. It is still very troublesome to pass too many API interfaces, at least it is tiring to maintain the json file.

It cannot support concurrent operations and can only be used for functional testing and pressure testing.

In short, this automated interface testing framework, and the framework with CI is more suitable for API interface-based testing. It is very easy to use, familiar with, and easy to maintain. I hope it can really help you.

  As someone who has been here, I also hope that everyone will avoid some detours

Here I will share with you some necessities of the way forward in automated testing, hoping to help you.

(WEB automated testing, app automated testing, interface automated testing, continuous integration, automated test development, big factory interview questions, resume templates, etc.)

I believe it can make you better progress!

Click on the small card below

Guess you like

Origin blog.csdn.net/Free355/article/details/131721807