preface

If you master HTTP, you master interface testing

I’ve seen many interface testing tutorials on the Web that start with how to use the tool without telling the reader why. The reader may have followed suit and succeeded, or he may have failed and somehow gone wrong.

Therefore, this article, as the first lesson of the interface test, will first give you to understand the purpose and principle of each step of the interface test operation, so that you can master the interface test from theory to practice, and have their own creative play after proficiency.

The interface test tool used in this article is Apifox, please install and register the tool before reading.

Download Apifox at www.apifox.cn

directory

  1. What do interface tests measure
  2. Interface test object: server interface
  3. Communication between the server interface and the front end: HTTP protocol
  4. Read APi documentation
  5. Apifox Interface test principle
  6. Build the first interface request with Apifox

The body of the

What do interface tests measure

Interface test is mainly a test of indirect interface of system component, mainly used to test the data interaction interface between server and front end (Web browser, APP). The focus of the test is to check the correctness of the interface parameter transfer, the correctness of the interface function realization, the correctness of the output results, and the integrity and rationality of the fault-tolerant processing of various exceptions.

It can be seen from the definition of interface test in the citation that the object of interface test is the server interface, and the content of interface test includes: interface parameter verification, whether the interface function is realized correctly, and fault-tolerant handling of interface exceptions.

The content of interface test is the content after “Apifox Interface Test” series tutorial, this article mainly explains the server interface, how the front end communicates with the server through HTTP protocol, how Apifox and other interface testing tools implement interface test.

Interface test object: server interface

An API is a set of predefined functions that allow the system or other external components to perform functions that it implements internally without having to access the source code or understand its internal logic.

During requirement development, the front and back ends are developed separately. Developers at both ends define interfaces and compile interface documents, which need to be followed in subsequent development.

Therefore, interface testing is also a kind of black box testing. Testers need to test the server Api according to the interface document to check whether the functions of the convention are correctly implemented and whether there is fault tolerance for exceptions.

The interface document is written according to the protocol of data transmission at the front and back end – Http protocol.

To be able to use interface documentation, first understand the HTTP protocol.

Communication between the server interface and the front end: HTTP protocol

The server and the front-end exchange data by sending HTTP packets to each other. This section describes how to create AN HTTP packet and how to understand each part of the packet. After understanding the packet, you can master the basic content of the interface test.

  • Three components of an HTTP packet An HTTP packet is a formatted data block. Packet types include client request and server response. They consist of three parts:

    • Start line Describes the packet
    • The header block contains the attribute content-type: content-length:
    • The body contains text or binary data and can be null
  • Format of the request packet

<method><request-URL><version>
<headers>
<entity-body>
Copy the code
  • Format of the response packet
<version><status><reason-phrase>
<headers>
<entity-body>
Copy the code
  • The meanings of each field are as follows:

Method: Operations that the front end wants to perform on the server, including get, POST, PUT, and DELETE.

methods function
GET Get data from the server
POST Sends data to the server for processing
HEAD Get only the header of the document from the server
DELETE Delete data from the server
PUT Submit data to the server

Request-url: The path to the requested resource. You can find the location of the resource by using this path. Format is similar to: www.apifox.cn/help/app/co…

Version: Indicates the HTTP version used by the packet. The format is similar to HTTP/1.0

The first (headers): Can have zero or more headers. Common headers are as follows:

Entity-body: Contains a block of data that supports multiple data formats, such as HTML pages, images, videos, source code, and so on.

Status-code: indicates the request result, success or failure.

Reason-phrase: A reason phrase is a readable version of a status code that is only meaningful to humans.

These fields will be very abstract and difficult for beginners to understand and remember, but when they come into contact with API documents and do interface tests with Apifox, they will have an Epiphany — parameters and methods in API documents, the meaning of URL and what to fill in each space in the interface test interface, and what the meaning of the return value represents.

Please bear with me and read on.

Read API documentation

For testers, the documentation needed to prepare interface tests is the product requirements document +API document.

The requirements document is used to tease out why the interface is designed the way it is and whether it makes sense.

For a single interface, the interface document is used to obtain: interface description, request description, return description.

Take Baidu open API and text recognition interface as an example: If we want to use its text recognition function, we need to call its interface, so we need to know what needs to be filled in each field to initiate this text recognition interface request.

* Note: Apifox’s ApiHub collects a large number of open apis. Beginners can select an API for in-depth understanding of interface documents, or as practice materials for interface testing. Note that some interfaces need to obtain permission to use first.

Open apis collected by the Api Hub

Apifox Interface test principle

For the server, Apifox is also a front-end, except that other front-end interface requests are wrapped in code by the developer and triggered to initiate a request under certain conditions, whereas in Apifox, interface requests are manually encapsulated and initiated by the user.

Apifox interface

After looking at the Apifox interface test, you can see that the whole interface is to manually construct an HTTP request, and the abstract HTTP protocol that we talked about in the last two sections is finally out of the way.

So one of the most basic HTTP interface testing steps is to manually complete and issue an HTTP request to validate the parameters.

Step1. Select request method -> fill in request url-> fill in url parameters -> fill in body parameters and header parameters (if any)

Step2. Manually send the request

Step3 check whether the returned parameters are normal and conform to the conventions of interface documents

Build your first test request with Apifox

After laying all the theoretical foundations mentioned above, you can finally start using Apifox to do interface testing.

Exercise 1: Get the HTML page with a GET request

step1: In apifox interface TAB, enter”www.baidu.com “, request method select GET, header, URL, body parameters are empty, then click the send button. step2: Check the returned value, you can see that the requested data is an HTML page, namely baidu home page. Click “Preview” button, you can see a complete page displayed

Click on the preview TAB

Exercise 2: Obtain the ACess_token of Baidu open API, that is, obtain the authorization to use API

Step1: Check the interface document of Baidu open API to obtain ACess_token and obtain the request method and request parameters

step2: Select the post method in the apifox interface test interface and fill in the params with 3 request parameters (client_id and client_secret can be obtained only after the application is created.

Fill in request parameters

step3: Click ** “actual request” ** TAB below, you can see the actual interface request form of Apifox:

step4Acess_token = acess_token;

Exercise done. You can use your own internal interface documentation/external open apis to continue to practice consolidation.

Review questions

1. Write the HTTP request packet and its format, and explain the meaning of each field.

2. What are the components of an API document and what are their roles in interface testing

3. Repeat the steps of simple interface test with Apifox.