One 、 Introduce
postman Provide powerful Web API & HTTP Request debugging , It can be used for interface testing .Postman It is divided into native app and chorme plug-in unit 2 Kind of ,chorme Plug ins are no longer maintained , The article uses native app. Download address :https://www.postman.com/postman/
Two 、 Send a request
1. Send a GET request
The weather api The address of :https://tianqiapi.com/api
2. Send a POST request
Get Netease News api The address of :https://api.apiopen.top/getWangYiNews
3、 ... and 、Cookie operation
If it's a page you can see only after you log in , You need to add cookie, Otherwise, you can't stay logged in . Optional on the requested headers Add cookie Or in cookie Manage page add cookie. Or make a login request first ,Postman Will automatically record Cookie, If you ask for the page again, you will succeed . Take the background home page for example :
Four 、 Variable
In the process of interface testing, parameters that have been set or changed parameters returned from other interfaces are often used ,Postman Provides a mechanism for variables to be used in this scenario .Postman Variables are divided into environment variables and global variables , In the interface test, you can choose a set of environment variables to use , Global variables are available to all interfaces . Use variables in the form of {{ Variable name }} Call variables
Return results
5、 ... and 、 Test sandbox
Postman Test sandbox is to use JS The script implements part of the test operation before and after sending the request , Common operations :
- Pre-request script (Pre-request-Script): Set request pre operation, such as setting variables .
- Assertion (Tests): Response code after request 、 Response head 、 Assertion operation in response to the subject information .
- Output information to console , Debugging information .
1. Pre test scripts , Take the weather interface as an example
After executing the interface request
2. Assertion
3. Common assertions Response body : JSON value check
6、 ... and 、 Test set
Postman Through the test set (collection) To easily manage interface requests . Think of each interface request as a use case ,collection Manage multiple use cases at once and execute them together . adopt Save Save the interface request to the test set .
Execution results
7、 ... and 、 Data driven
It is not possible to test only one set of data , An interface needs to test multiple sets of data , You need to use data-driven . Data driven support .csv and json Format , With .csv Format as an example , You can start with Excel Write in , And save it as .csv Format , But the encoding format is not correct , It can be used notepad++ open , Turn into utf-8 code
Data driven data
Take the weather interface as an example , Variable and assertion settings
Execute interface test set settings
8、 ... and 、Newman
postman Use Newman The plug-in completes the command line execution script ,newman Official documents of https://www.npmjs.com/package/newman
1. install Newman
install Newman You need to install nodejs, On the official website http://nodejs.cn/download/ Download the file , Then click Run install , After installation , Enter at the command line node -v and npm -v Check whether the environment is successfully built , Otherwise, manually add environment variables
Then install Newman:npm install newman -g
2. Export test set file
3. Execute the script
4. Common parameters
nenman Some operations can be done with parameters , Please refer to newman Official documents of https://www.npmjs.com/package/newman perhaps newman run --help
newman Common parameters of :
- -e, --environment: Use environment variable files or url. First, the environment variables are exported through the export function in the environment variables , Then execute the script
- -g, --global : Use global variable files or url
- -d, --iteration-data : Using data driven files
- -n: Specifies the number of test set cycles iterations
- -r, --reporter:newman Provide 3 Report form (cli,json,html),cli Default format , Display results directly on the command line
5. Timing execution newman Script
Create a batch file (.bat), It can be executed by double clicking
utilize windows Timed tasks, timed scripts , You can also do it in jenkins Integration tasks in .
Right click the computer icon on the desktop -- Selective management -- Select task scheduler -- Select Create Task , Create a scheduled task , Then it's time to automate the task
Nine 、Postman Grab the bag
Ten 、Postman Automatically generate request scripts