当前位置:网站首页>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 ...
边栏推荐
- 一碰撞就自燃,谁来关心电池安全?上汽通用有话说
- Vivado design single cycle CPU
- 太空游戏第12课: 盾牌
- Usage Summary of thymeleaf
- 【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)
- [C language] use function pointers to make a different calculator
- Linux Installation MySQL (pit filling version)
- C语言简单实例 1
- [task02: SQL basic query and sorting]
- DSACTF7月re
猜你喜欢
![[C language] 5000 word super detailed explanation of various operations of the sequence table](/img/3b/932526e96ef14df8a321048e9c14b4.png)
[C language] 5000 word super detailed explanation of various operations of the sequence table

CNN convolution neural network learning process (weight update)
Database tuning - connection pool optimization

js网页黑白背景开关js特效

树行表达方式

同质化代币与 NFT 结合,如何使治理结构设计更灵活?

C language data 3 (1)

C language - data storage

MySQL startup error 1607 unexpected process termination

Windows系统下Mysql数据库定时备份
随机推荐
Solve the problem that the nocturnal simulator cannot access the Internet after setting an agent
关于正则的两道笔试面试题
Raspberrypico analytic PWM
太空射击第16课: 道具(Part 2)
The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution
Extract China map from global.Nc data and calculate regional CO2 value based on acgis
Windows系统下Mysql数据库定时备份
树行表达方式
Solve the problem that jupyter cannot import new packages
Does any elder brother know how to solve the huge flinksql log
进制及数的表示 2
js网页黑白背景开关js特效
弹出模态框
One article makes you understand what typescript is
New fruit naming (DP is similar to the longest common subsequence)
[dynamic link library (DLL) initialization example program failed "problem]
一文让你搞懂什么是TypeScript
关于链接到其他页面的标题
MySQL error: specified key was too long; max key length is 767 bytes
字符设备驱动结构