当前位置:网站首页>Sorting out problems in interface endpoint testing practice using Supertest
Sorting out problems in interface endpoint testing practice using Supertest
2022-07-28 20:36:00 【Xiao language is scattered】
Use Supertest Sorting out the problems of interface endpoint testing practice
One 、 Preface
The function of endpoint testing is when you develop some API Interface , If there is no time to conduct a large number of manual interface tests , Then we need some automatic tools to automatically test these interface functions ,supertest Its function is to let you write some scripts to realize the automatic test of the interface .
about supertest The installation and basic usage of are not explained too much in this article , This article mainly sorts out some of the things I'm using supertest Some pits encountered in the process , Sorted out some solutions , I hope it can help you , Articles may be continuously updated , There is no end to the comparison pit ,bug It can't be finished ...
Two 、 test GET Class interface ID Value transfer problem
Get The interface of a class generally requires some condition parameters , Resource like ID, User ID Parameters like that , This kind of parameter usually has two transfer forms ,
- Mount directly in the address of the interface , Write in url in , for example “https://www.test.com/api/:testId” In the form of ;
- When requesting the interface, hang the parameters to the requested query Inside , Spell the parameters , for example “https://www.test.com/api?testId=123456” In the form of .
So how should these two forms be written when writing endpoint test scripts ?
First , The first form , Direct will ID Data write in url in , The code is as follows :
request(app).get("https://www.test.com/api/",testId);
request(app).get(`https://www.test.com/api/${testId}`);
Both methods can meet the requirements .
The second form , Hang the data to url Behind , When I first started writing, I wrote several wrong forms , Because the official documents are not very clear , Other documents do not explain this content , It led me to spend a long time here ...
Now let me first write some wrong forms I wrote , The code is as follows :
request(app).get("https://www.test.com/api")
.query("testId", testId);
request(app).get("https://www.test.com/api")
.query({testId: testId});
First of all, these two forms are incorrect , This form is useless request.query.testId To receive testId Data. , The correct way is as follows :
request(app).get("https://www.test.com/api")
.query(`testId=${testId}`);
Yes , You need to write the complete string into query In the way of , It's that simple , I don't know why I got stuck in this place ... I hope others don't take detours
3、 ... and 、 test POST Data transmission of class interface
Post Class interface , Data transmission is relatively simple ,data It is usually hung on the request body Inside , Write directly into the test script send It's OK in the method , The code is as follows :
request(app).post("https://www.test.com/api")
.send({
name: "sage"
});
Four 、 test PATCH Class update interface value passing problem
The update interface is generally the update of resources , First, you need to import resources ID To find resources , Then update the resources according to the incoming data , Two forms , The code is as follows :
request(app).patch(`https://www.test.com/api/${testId}`)
.send({
name: “sage”
});
request(app).patch("https://www.test.com/api")
.query(`testId=${testId}`)
.send({
name: "sage"
});
Keep rolling , Step on the pit more than , Post update ...
边栏推荐
- 关于链接到其他页面的标题
- Use of DDR3 (axi4) in Xilinx vivado (5) board test
- Mysql报错:Specified key was too long; max key length is 767 bytes
- 七种轮询介绍(后附实践链接)
- TCP.IP
- [task01: getting familiar with database and SQL]
- [experiment sharing] CCIE BGP reflector experiment
- Introduction to seven kinds of polling (practice link attached)
- Raspberry pie CM4 -- using metartc3.0 to integrate ffmpeg to realize webrtc push-pull streaming
- Shanghai Jiaotong University joined hands with Taobao to set up a media computing laboratory: promoting the development of key technologies such as video super score
猜你喜欢
Maximum exchange [greedy thought & monotonic stack implementation]
Speech controlled robot based on ROS (I): realization of basic functions
CM4 development cross compilation tool chain production
DOS common commands
弹出模态框
js网页黑白背景开关js特效
Linux Installation MySQL (pit filling version)
C language - data storage
Nocturnal simulator settings agent cannot be saved
[task01: getting familiar with database and SQL]
随机推荐
User, user group related operations
DOS common commands
Voice controlled robot based on ROS (II): implementation of upper computer
[dynamic link library (DLL) initialization example program failed "problem]
js图表散点图例子
Nocturnal simulator settings agent cannot be saved
Solve the kangaroo crossing problem (DP)
Linxu [permission, sticky bit]
Use of DDR3 (axi4) in Xilinx vivado (2) read write design
Torch. NN. Linear() function
Solve the brick stacking problem (DP)
Read JSON configuration file to realize data-driven testing
C语言数据 3(1)
漂亮的蓝色背景表单输入框样式
太空射击第11课: Sound and Music
CNN convolutional neural network structure
LeetCode_ Bit operation_ Medium_ 260. Number III that appears only once
Speech controlled robot based on ROS (I): realization of basic functions
File lookup and file permissions
Explain RESNET residual network in detail