Preface
掌握了http协议,就掌握了接口测试
The author has seen many interface testing tutorials on the Internet, and started to talk about how to operate the tool as soon as it came up, without telling the reader why it should be done. The reader may have succeeded in drawing the cat and the tiger, or the operation may have failed but somehow it went wrong.
Therefore, as the first lesson of the introduction to interface testing, this article will first give you an understanding of the purpose and principle of each step of performing interface testing, so that you can master interface testing from theory to practice, and have your own creativity after proficiency.
The interface testing tool used in this article is Apifox, please install and register the tool before reading down.
Apifox official download address: http://www.apifox.cn
directory
接口测试测什么
接口测试的对象:服务器接口
服务器接口与前端通信的方式:http协议
Apifox接口测试原理
用Apifox构建第一个接口请求
text
1. What to test for interface testing
接口测试主要是测试系统 组件间接口的一种测试,主要用于测试服务器与前端(web浏览器,APP)之间的数据交互接口。 测试的重点是要检查接口参数传递的正确性,接口功能实现的正确性,输出结果的正确性,以及对各种异常情况的容错处理的完整性和合理性。
From the definition of the interface test in the quotation, the object of the interface test is the server interface. The content of the interface test includes: interface parameter inspection, whether the interface function is implemented correctly, and the fault-tolerant handling of interface abnormalities.
The content of the interface test is the latter part of the "Apifox Interface Test" series of tutorials. This article mainly explains the server interface, how the front-end communicates with the server through the http protocol, and how the interface test tools such as Apifox implement interface testing.
2. The object of the interface test: server interface
API refers to a set of pre-defined functions that allow the system or other external components to perform its internally implemented functions without having to access the source code or understand its internal logic.
In the requirements development process, the front and back ends are developed separately, and the R&D personnel at both ends will jointly define the interface and write the interface document. This agreement document needs to be followed in the subsequent development process.
Therefore, the interface test is also a kind of black box test. Testers need to test the server Api according to the interface document to check whether the agreed function is implemented correctly and whether there is fault tolerance for abnormal situations.
The compilation of the interface document is based on the Http protocol, the front-end and back-end data transmission protocol.
To be able to use interface documents, first understand the http protocol.
3. The way the server interface communicates with the front end: http protocol
The server and the front end complete data interaction by sending http messages to each other. This section will introduce how to create an http message and how to understand each part of the message. After understanding, you can master the basic content of interface testing.
The three components of the http message The http message is a formatted data block. Message types include client request and server response. They consist of 3 parts:
起始行(start line) 对报文进行描述
首部块(header) 包含属性 Content-Type: Content-Length:
主体(body) 包含文本或二进制数据,可以为空
Request message format
<method><request-URL><version>
<headers>
<entity-body>
Response message format
<version><status><reason-phrase>
<headers>
<entity-body>
meaning of each field is as follows:
method (method) : The operation that the front-end wants to perform on the server, including get, post, put, delete and other methods;
Method function
GET to get data from the server
POST sends the data to be processed to the server
HEAD only gets the header of the document from the server
DELETE deletes data from the server
PUT submits data to the server
Request url (request-URL): The requested resource path, through which the location of the resource can be found. The format is similar to: https://www.apifox.cn/help/app/contact-us/
Version (version): The http version used by the message, the format is similar to: HTTP/1.0
: There can be zero or more headers, the common headers are as follows: Common header 1619cd0cb3a952
Entity-body: Contains a data block that supports multiple data formats, such as html pages, pictures, videos, source code, etc.
Status code (status-code): Describe the result of the request, success or failure.
Reason-phrase: Reason-phrase is a readable version of the status code, which is only meaningful to humans.
Beginners of these fields will find it very abstract and difficult to understand and remember, but when they come into contact with the api document and use apifox for interface testing, they will have an epiphany-the parameters and methods in the api document, the meaning of the url and the interface test interface. What do you need to fill in a blank, and what does the return value mean?
Please be patient and continue reading.
4. Read API documentation
For testers, the documents needed to prepare interface tests include product requirements documents + API documents.
The requirements document is used to sort out why the interface is so designed and whether it is reasonable;
For a single interface, the interface document is used to obtain: interface description, request description, and return description.
Take Baidu's open API and text recognition interface as an example: if we want to use its text recognition function, we need to call its interface, then we need to understand what content needs to be filled in each field to initiate this text recognition interface request.
*Note: In Apifox's ApiHub, a large number of open APIs are collected. Beginners can choose an api to learn more about the interface documentation or use it as an exercise material for interface testing. Note that some interfaces need to be used first.
Open API collected by Api Hub
Five. Apifox interface test principle
For the server, Apifox is also a front-end, but the interface requests of other front-ends are encapsulated in the code by the developers and are triggered to initiate the request under certain conditions. In Apifox, the interface request is manually encapsulated and initiated by the user.
Apifox interface
Apifox interface
After reading the interface of the Apifox interface test, readers must be able to find that the entire interface is for the reader to manually construct an http request. The abstract http protocol we talked about in the previous two sections has finally landed.
Therefore, the most basic http interface test requires the steps to be completed manually and send an http request to verify the parameters.
step1. Select the request method -> fill in the request url -> fill in the url parameter -> fill in the body parameter and header parameter (if any)
step2. Send the request manually
step3. Check whether the return parameter is normal and whether it conforms to the convention of the interface document
Build your first test request with Apifox
After laying out the above theoretical foundations, everyone can finally start to use apifox for interface testing.
Exercise 1: Get html page with get request
http://www.baidu.com" in the new interface tab of apifox, and select GET as the request method, and the header parameters, url parameters, and body parameters are all empty, and then click the send button. step2: Check the return value, you can see that the requested data is an html page, that is, the Baidu homepage, we click
Exercise 2: Obtain the access_token of Baidu's open API, that is, obtain the authorization to use the API
step1: Check the interface document of Baidu open api to get access_token, get the request method and request parameters
Interface document description
step2: According to the description of the interface document, select the post method in the apifox interface test interface, and fill in the 3 request parameters in params (client_id and client_secret can only be obtained after the application is created, if not, you can create it first)
Fill in the request parameters
Fill in the request parameters
step3: Click the actual request tab below, you can see the interface request form actually sent by apifox is as follows: apifox sent the actual request
step4: Check the return parameters to see that the acess_token field we need is returned:
The exercise is complete. You can use your own company's internal interface documentation/external open api to continue to practice and consolidate.
Review Questions
1. Write the HTTP protocol request message and the format of the corresponding message, and explain the meaning of each field.
2. What are the components of the api document, and what roles do they play in interface testing?
3. Retell the steps for simple interface testing with Apifox.
*"Apifox Interface Testing Tutorial" series of tutorials are in serialization, so stay tuned!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。