Interface Automation testing using Postman+newman+python

Source: Internet
Author: User
Tags python script

Postman is an API debugging tool that can be used to test interfaces, similar tools have JMeter, Soupui. Through the Postman+newman+python can run the debugging interface in bulk, to achieve the effect of automated testing.

1, Postman Installation
There are two ways, one is the plugin on Chrome browser, one is postman client. I am using the postman client.
1) How to install postman in Chrome browser
Https://www.cnblogs.com/mafly/p/postman.html
2) Install Postman Client
A, download software Https://www.getpostman.com/apps
B, installation
1. function
1) Send request, view response
2) environment variables, global variables
Environment variables: only for settings
Set Environment variables: Pm.environment.set ("Variable_key", "Variable_value");
Get environment variable: pm.environment.get ("Variable_key");
Global variables: Acting on all environments
Set Global variables: Pm.globals.set ("Variable_key", "Variable_value");
Get global variable: pm.globals.get ("Variable_key");
Examples of Use:
var data=json.parse (responsebody);
var Act=data.data.accesstoken;
Postman.setglobalvariable ("Accesstoken", act);
Postman.setglobalvariable ("UserId", data.data.userId);
Postman.setglobalvariable ("Refreshtoken", Data.data.refreshToken);
var afterurl= "access_token=" +act+ "&userid=" +data.data.userid;
Pm.globals.set ("Afterurl", Afterurl);
Console.log (Afterurl)
Using variables:
Replace with {{variableName}} in the variable place used
Specific View document: Https://www.getpostman.com/docs/postman/environments_and_globals/variables
3) Set Assertion
tests["Your Test nickname"] = Data.data.nickName = = = "2589"//response content nickname =2589
Pm.test ("Status code is", function () {
Pm.response.to.have.status (200);
}); returns to 200

var responsejson=json.parse (responsebody);
tests[' response matches the data posted '] = (responsejson.data && responseJSON.data.length = = 10);
Return Data Total 10 records
4) Commissioning the console
Need to be in Postman client, click View->show Postman Console to recall
Write scripts in test or pre-request script to print out questionable values
Console.log (VariableName); Run the request after
5) Collection
Need to be on postman client, click Collection->runner, Run
Specific View document: Https://www.getpostman.com/docs/postman/collection_runs/starting_a_collection_run

6) specific use such as

\

7) Export JSON file

2, Newman Installation

Official Help document Address: Https://www.npmjs.com/package/newman
1) need to install Nodejs and configure the environment.
2) Open the console and run: NPM install-g Newman
3) Check whether the installation is successful, run: Newman--version
Newman Execute Script
Newman made a big change after the 3 release, but running the commands is becoming more and more simple as follows:

Newman run <collection-file-source> [options]
Run behind the JSON file or URL that you want to execute (both JSON and URL are generated by postman export), followed by parameters such as environment variables, test reports, interface request timeouts, and so on. Finally, two complete examples are given as a reference:
Newman run D:/buddy_product_enviroment.postman_collection.json--reporters cli,html,json,junit-- Reporter-json-export D:/jsonout.json--reporter-junit-export d:/xmlout.xml--reporter-html-export D:/htmlOut.html

3. Execute Newman using Python script

 # coding=utf-8 
Import time
Import OS
class Postmanapitest:
#运行postman生成报告
#通过newman

def postman (self):
Jsonfname = ' d:/htmlout ' + time.strftime ('%y-%m-%d ', Time.gmtime ()) + '. html '
# cm d = ' Newman run? D:/buddy_test_enviroment.postman_collection.json--reporters cli,html,json,junit--reporter-html-export ' + Jsonfname
Cmd= ' Newman run D:/buddy_product_enviroment.postman_collection.json--reporters Cli,html,json,junit- -reporter-json-export D:/jsonout.json--reporter-junit-export d:/xmlout.xml--reporter-html-export D:/htmlOut.html '
Os.system (cmd)
print ('------------------------------------------------------------')
pri NT (jsonfname)
if Os.path.isfile (jsonfname):
return jsonfname
Print (jsonfname)
else:
return False
If __name__ = = ' __main__ ':
a=postmanapitest ()
A.postman ()
The Br>4 and final generation report is as follows:

Interface Automation testing using Postman+newman+python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.