Use postman as rest api automated testing tool.

  postman, testing

Order

After writing more api or taking over other people’s projects, there will be more operation and maintenance of api, especially in the test environment, various factors will lead to the interface not meeting expectations. At this time, when products and tests come to harass you, your first reaction is to perform it yourself, see if there is a problem with the interface, and then analyze it concretely.

Usually, the interface address is spliced, the parameters are constructed, and then the api is requested to see the returned result. After doing too many such actions, it is usually annoying, especially when the interface is ok.

Therefore, I want to find an automated testing tool for rest api to help me troubleshoot problems.

Tool selection

Selection criteria

  • Swagger can be imported in batch.

  • Able to construct test interface by oneself

  • Run in batch

  • Ability to output report

  • It is better to alert

Candidate tool

  • Jmeter-found a circle online, the first one to enter the eye was JMeter, but due to the rough interface and high learning cost, it was not considered for the time being.

  • Soupui-looks like an old-fashioned tool, but since the free version does not support exporting report, it gave up.

  • Dredd-This looks good and can support swagger, but I gave it a rough try and gave it up first if it didn’t work out.

  • Postman-this has been installed before, but I didn’t find the function of digging deep into it. now, it is quite simple and easy to use. it also supports swagger, and then it is

Postman related knowledge

Import swagger

This function is what I value most. There is an import button in the upper left corner. You can select “Import From Link” and enter the address of swagger api docs of the interface, such as:http://192.168.99.100: 8080/scm/v2/api-docs, then import it.

Set collection

When saving a request, you can select an existing collection or create a new one. Collection can be equivalent to test suite.

Set tests

For those who want to run automated tests, the scripts here must be set up, otherwise even if they run, they will have no meaning. The simplest and most common two-line script is as follows:

tests["Status code is 200"] = responseCode.code === 200; 
tests['Response time is less than 500ms'] = responseTime < 500;

One is the status code that asserts http, and the other is the assertion response time.

Setting environment variables

Select an http request from collection casually, then click the set button on the right to Manage Environments, and then you can define environment variables, dev and prod, respectively, to set the host of api for the corresponding environment, so you don’t need to set api requests repeatedly. Use {{varname}} to refer to a variable in the url, assuming varname is a variable name you set.

Runner execution

There is a runner icon in the upper left corner. Click to pop up the COLLECTION RUNNER interface. In the place where batch automatic testing is to be carried out, select environment and run.
图片描述

Newman command line execution

  • Export configuration
    At collectionwhere, export, select collection and export as json. If environment is used, you need to export json configuration of the environment.

  • Install newman

sudo npm install -g newman
  • See newman version

newman -version
3.4.3
  • Command line execution

newman run demo.postman_collection.json --reporters cli,html --environment dev.postman_environment.json  --reporter-html-export result.html

The command line results are as follows:


┌─────────────────────────┬──────────┬──────────┐
│                         │ executed │   failed │
├─────────────────────────┼──────────┼──────────┤
│              iterations │        1 │        0 │
├─────────────────────────┼──────────┼──────────┤
│                requests │       22 │        0 │
├─────────────────────────┼──────────┼──────────┤
│            test-scripts │       22 │        0 │
├─────────────────────────┼──────────┼──────────┤
│      prerequest-scripts │        0 │        0 │
├─────────────────────────┼──────────┼──────────┤
│              assertions │       44 │        6 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 28s                       │
├───────────────────────────────────────────────┤
│ total data received: 312.29KB (approx)        │
├───────────────────────────────────────────────┤
│ average response time: 1245ms                 │
└───────────────────────────────────────────────┘

  #  failure        detail

 1.  AssertionFai…  Response time is less than 1000ms
                    at assertion:2 in test-script
                    inside "XXXX" of "app1"

 2.  AssertionFai…  Response time is less than 1000ms
                    at assertion:2 in test-script
                    inside "XXXX" of "app2"

 3.  AssertionFai…  Response time is less than 1000ms
                    at assertion:2 in test-script
                    inside "XXXX" of "app1"

 4.  AssertionFai…  Response time is less than 1000ms
                    at assertion:2 in test-script
                    inside "XXXX" of "app3"

 5.  AssertionFai…  Response time is less than 1000ms
                    at assertion:2 in test-script
                    inside "XXXX" of
                    "app2"

 6.  AssertionFai…  Status code is 200
                    at assertion:1 in test-script
                    inside "XXXX" of "app1"

At the same time, a result.html report will be generated.

Jenkins integration

  • Build and select EXECUTE WINDOWS BATCH COMMAND-Just enter the above command

  • Published junit testresultreport-jenkins has a published junit testresultreport that can be used to parse junit’s xml test report. To use this, the command line has to output junit’s report

newman run demo.postman_collection.json --reporters cli,html,junit --environment dev.postman_environment.json  --reporter-html-export result.html --reporter-junit-export junit-result.xml

doc