当前位置:网站首页>Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
2022-07-06 12:16:00 【A pole】
Postman
First of all, introduce a way to transfer to Chinese version ,GitHub link :https://github.com/hlmd/Postman-cn
Catalog
Preface
This paper is about postman Intermediate use tutorial , Suitable for a certain postman Basic friends read , If not used postman Or similar software , Reading this article may be uncomfortable .
environment variable
New environment variable
Defined global variables and environment variables , It can be referenced in the interface .
When adding an environment, you can click the plus sign in the upper left corner , Then add the public variables you want to use in the environment .
This function is generally used for things like url Prefix
This kind of variable will be reused in many places .
The reference method is to put the variable you want to reference between two curly braces , for example :{ {url}}
Like this :
Of course, if you need to switch in multiple environments , There is no need to always modify url Value , You can create several more environment variables , Then switch quickly in the upper right corner :
Switch environment variables
Click the small eye to see what variables are in the environment you are currently using :
Interface Association
In fact, it also uses global variables
Write test script
Pre request script and test script can be written js Code , The difference between them is , The pre request script is run before sending the request , The test script is run after sending .
Get the data in the test script and set it as a global variable , For example, when logging in token:
var result = postman.getResponseHeader("token");
console.log(result);
pm.globals.set("token", result);
The global variables set in this way token Can be used on other interfaces , Use { {token}}
To reference variables .
Of course , You can also use regular expressions to get variables , Just use it js Write regular , There is not much here .
Dynamic parameters
Built in dynamic parameters
- { {$timestamp}} Generate timestamp of current time
- { {$randomint}} Generate 0-1000 Between random numbers
- { {$guid}} Generate shorthand GUID character string
Custom dynamic parameters
Pass in the pre request script js Code to set variables , Use when using { {times}} To get variables , It is similar to the test script written in Interface Association .
var times = Date.now();
pm.globals.set("times", times);
How to get global variables :
pm.globals.get("token", result);
global["token"];
global.token;
Assertion
Assertions can be used to determine whether the request is successful , Used for automated testing .
postman Support multiple assertion methods , On the right side of the test script, you can see , Click the blue word to get the code directly :
Here are some common :
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
MokeService
Create an impersonation server . Before the back-end interface is developed , The front end wants to call the interface , You can use it first postman Simulation server functions provided .
Create an impersonation server , Just set the request name ,postman It will automatically generate a url For your temporary use , The url It can be accessed directly through the external network , But the returned data is fixed , But it's enough for the front-end to use temporarily .
Write interface documentation
There is a document button on the right side of each interface , Click to edit the description of the current interface :
The tested interface and written documents can directly generate documents , You can see :
Then there is a Publish button in the upper right corner of the document , Clicking publish seems to generate online documents , But I haven't used .
Save test samples
Send the completed request , You can save the request results , Provide reference for others .
The saved example will be displayed in the left set , Placed under this interface .
Conclusion
The above content only describes the general method of use , You still need to use it yourself , If this article helps you , Welcome to my collection .
边栏推荐
- 小天才电话手表 Z3工作原理
- Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
- 冒泡排序【C语言】
- OPPO VOOC快充电路和协议
- Who says that PT online schema change does not lock the table, or deadlock
- 基于Redis的分布式ID生成器
- arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
- C语言回调函数【C语言】
- Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
- Pytorch实现简单线性回归Demo
猜你喜欢
Several declarations about pointers [C language]
程序员老鸟都会搞错的问题 C语言基础 指针和数组
Basic operations of databases and tables ----- view data tables
Common properties of location
Unit test - unittest framework
open-mmlab labelImg mmdetection
JS 函数提升和var变量的声明提升
[esp32 learning-1] construction of Arduino esp32 development environment
AMBA、AHB、APB、AXI的理解
Walk into WPF's drawing Bing Dwen Dwen
随机推荐
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
Pytorch four commonly used optimizer tests
Basic knowledge of lithium battery
Amba, ahb, APB, Axi Understanding
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
OPPO VOOC快充电路和协议
2022.2.12 resumption
ES6语法总结--下篇(进阶篇 ES6~ES11)
Togglebutton realizes the effect of switching lights
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
MySQL takes up too much memory solution
Working principle of genius telephone watch Z3
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
gcc 编译选项
PT OSC deadlock analysis
Unit test - unittest framework
Characteristics, task status and startup of UCOS III