当前位置:网站首页>Introduction to API testing
Introduction to API testing
2022-07-26 01:17:00 【Abstinence Li Bai -lisage】
Nowadays, the testing strategy of Internet products often adopts diamond structure , Heavyweight API test , Lightweight GUI test , Lightweight unit testing , thus it can be seen API The importance of testing in today's testing is self-evident . This paper will design a test case , Through this test case, we can feel API How the test is conducted , And introduce several commonly used testing tools .
Catalog
Two 、 Use cURL Command line tools to test
3、 ... and 、 Use Postman Graphical tools for testing
One 、API testing procedure
- Test data
- adopt API Testing tools , Send to server requests request
- The server receives the message based on response Respond to
Yes API Tests often use API Testing tools , For example, common command line tools cURL、 GUI tools Postman perhaps SoapUI、API Performance test JMeter etc. .
Two 、 Use cURL Command line tools to test
First , We need to download and install cURL, Then it can be initiated by the following command Account API Call to . Here's the picture
curl -i -H "Accept: application/json" -X GET "http://127.0.0.1:8080/account/ID008"

The meaning of the parameters in this line of command is as follows :
The first parameter “-i”, Instructions need to show response Of header Information ;
The second parameter “-H”, Used to set request Medium header;
The third parameter “-X”, Method used to specify execution , It's used here GET Method , Other common methods are POST、PUT and DELETE etc. , If you don't specify “-X”, So the default way is GET.
Last “ http://127.0.0.1:8080/account/ID008 ”, Indicates the tested API Of endpoint And specific ID The value is “ID008”.
When using cURL Conduct API When testing , There are two common parameters :
“-d”: Used to set http Parameters ,http Parameters can be added directly to URL Of query string, It can also be used. “-d” Bring in parameters . You can use “” concatenated , Or use multiple “-d”.
“-b”: When it's time to deliver cookie when , Is used to specify the cookie Path to file .
Note that these parameters are case sensitive .
After knowing the most commonly used parameters , Let me analyze some of the most commonly used cURL Command and usage scenarios , Include Session And Cookie Scene .
2.1 Session scene
If the backend engineer uses session Record user login information , Then the backend will usually send a session ID To the front end . after , The front end is sending to the back end requests Of header You need to set this session ID, The back end will use this session ID Identify which front end belongs to session, here cURL The command line for is as follows :
curl -i -H "sessionid:XXXXXXXXXX" -X GET "http://XXX/api/demoAPI"
2.2 Cookie scene
If using cookie, After successful certification , The back end will return cookie To the front end , The front end can put the cookie Save as a file , When you need to use the cookie when , Reuse “-b cookie_File” The way in request Middle implantation cookie It can be used normally . Concrete cURL The command line for is as follows :
// take cookie Save as a file
curl -i -X POST -d username=robin -d password=password123 -c ~/cookie.txt "http://XXX/auth"// load cookie To request in
curl -i -H "Accept:application/json" -X GET -b ~/cookie.txt "http://XXX/api/demoAPI"
3、 ... and 、 Use Postman Graphical tools for testing
Postman Is currently the most widely used Http One of the request simulation tools , Often used for Web Service API Test of . In the early Postman, In order to Chrome Browser plug-ins (plugin) In form , Latest version Postman It's an independent application .
Operation steps :
- launch API request
- Add parameter validation
- Save test cases
- Execute test case
3.1 launch API request
Our goal is to be right Account API Do a test , So here you need to choose Postmant Of “Request” modular . After entering the corresponding interface , You need to follow the prompts in the following three steps , launch Account API Call to .
- stay endpoint Enter... In the input box “http://127.0.0.1:8080/account/ID_008”;
- choice “GET” Method ;
- Click on “Send” Button launch API call .

After completing the above steps , We see the return response Default to JSON The form of the file is shown in Body in .

It's done once Account API Call to , Is it very simple . But the problem is , It's just a API call , There is no automated validation of the call results . Next , Let's add the verification part , Let's see what the effect is .
3.2 Add parameter validation
stay Postman It's also very convenient to add result validation to , Suppose we were in Account API There are four verification points in the test process :
- Requested return status code (Status Code) Should be 200;
- The response time of the request should be less than 200 ms;
- Requested returned response header It should include “Content-Type” Parameters ;
- Requested returned response body in ,“type” The value should be “friends”;
We can open our test Interface , And then in the lower right corner “SNIPPETS” Click... In turn :
- “Status code: Code is 200”
- “Response time is less than 200 ms”
- “Response headers:Content-Type header check”
- “Response body: JSON value check”
After finishing the above operation ,“Tests” The verification code will be generated automatically in , Then just follow the specific test requirements , Make some minor changes to the generated code .

3.3 Save test cases
This operation is very simple , In us postman Left side of tool , Provides Collection Collection tool , We can click Save As Buttons can create our Collection, And save the test cases we created .
3.4 Execute test case
seeing the name of a thing one thinks of its function , Our friendship also mentioned , Is to prepare the data 、 The process of initiating a request . stay postman Inside , Click on send button .
Four 、 summary
Usually , Whatever you use API Testing tools , The basic test steps are often three steps , Prepare the test data ( Not all API This step is needed for testing )、 adopt API The test tool initiates a test on the tested API Of request、 Verify that response.
Expand the thinking :
Single API The test is relatively simple , But in the real world , There are often sequential API Call and asynchronous API call , This kind of API How should it be tested ? How to solve it ?
边栏推荐
- Browser development and use skills
- Linear relationship between vectors
- Cross-lingual Transfer of Correlations between Parts of Speech and Gaze Features 阅读笔记
- Docker高级篇-Mysql主从复制
- What is the dynamic IP address? Why do you recommend dynamic IP proxy?
- Modify CSV
- 电视机软件烧录
- Should we test the Dao layer?
- [RTOS training camp] course learning methods and C language knowledge (pointer, structure, function pointer, linked list) and student questions
- 场景之分页查询设计
猜你喜欢

Mulda: a multilingual data augmentation framework for low resource cross linguistic ner reading notes

换ip软件的用途很广及原理 动态IP更换的四种方法来保护网络隐私

Embedded development: tips and tricks -- seven tips for designing powerful boot loader

1.30 upgrade bin file, add suffix and file length

游戏思考17:寻路引擎recast和detour学习二:recast导航网格生成流程及局限性

【纪中】2022.7.16 1432.输油管道

How to obtain the cash flow data of advertising services to help analyze the advertising effect?

Machine learning: Bayesian Networks

MulDA: A Multilingual Data Augmentation Framework for Low-Resource Cross-Lingual NER 阅读笔记

【软件开发规范三】【软件版本命名规范】
随机推荐
Cross linguistic transfer of correlations between parts of speech and Gazette Features Reading Notes
[software development specification III] [software version naming Specification]
Android SQLite first groups and then sorts left concatenated queries
Jushi | Haitai Fangyuan appears at the 5th Digital China Construction Summit
[software development specification II] prohibited item development specification
Spine_ Adnexal skin
Small sample learning - getting started
Regular expression
MulDA: A Multilingual Data Augmentation Framework for Low-Resource Cross-Lingual NER 阅读笔记
服务器可用资源查询脚本
动态IP地址是什么?为什么大家会推荐用动态ip代理?
Mmocr usage guide
《自然语言处理实战入门》深度学习基础 ---- attention 注意力机制 ,Transformer 深度解析与学习材料汇总
Transfer learning - getting started
链表相关面试题
REST-assured接口测试框架详解
【Go】如何控制协程的最大并发数
【软件开发规范二】《禁止项开发规范》
《nlp入门+实战:第三章:梯度下降和反向传播 》
Subarray with 19 and K