当前位置:网站首页>Interface test summary - it's worth seeing

Interface test summary - it's worth seeing

2021-08-10 08:14:08 Test Mengmeng


 Insert picture description here

Preface

Speaking of interface testing , There are many examples on the Internet , But as a novice, for me , I don't know what they're talking about , I think the interface test , How tall . I think I can counter attack the loser by learning interface testing , Reach the peak of life , Marry Bai Fumei . So after learning some development knowledge , I found that interface testing is actually a term people play . The interface test , It's really simple . It's just data transfer, it's just a performance .

Request model

Look at the picture below “A” Line , It can be understood that the interface is a light switch , It provides you with a parameter in the interface , The value of the parameter is “ open ”, One is “ Turn off ”.

 picture

To say , How can I make the light on ? One lamp cap is connected with two wires , A live wire , A line is connected to the neutral line, and the light is on . conversely , No neutral line 、 The firelight went out .

Actually, the interface , It simplifies these complex operations , All you see is a switch , And you just operate this switch . We only need to test this switch to complete the task when we do interface testing , Interface testing is that simple .

When you visit “http://127.0.0.1:8080/light?opt=open”, Let the zero line 、 Fire wire connection , The light is on .

When you visit “http://127.0.0.1:8080/light?opt=close”, Let the zero line 、 The live wire is disconnected , Now the lights are off .

Request structure

Seeing this, we can roughly understand what interface testing is all about . Next, we need to understand HTTP Of URL How to form an interface . Pictured :

 picture

1、 One URL It's an interface : The interface will be roughly divided into the following parts :

http Request mode

GET ---  By request URI Get resources 
POST ---  For adding new content 
PUT ---  Used to modify something 
DELETE ---  Delete something 
CONNECT ---  For proxy transmission , If you use SSL
OPTIONS ---  Ask what you can do 
PATCH ---  Some document changes 
PROPFIND (wedav) ---  View the properties 
PROPPATCH (wedav) ---  Set properties 
MKCOL (wedav) ---  Create set ( Folder )
COPY (wedav) ---  Copy 
MOVE (wedav) ---  Move 
LOCK (wedav) ---  Lock 
UNLOCK (wedav) ---  Unlock 
TRACE ---  For remote diagnostic server 
HEAD ---  Be similar to GET,  But not back body Information , Used to check whether an object exists , And get the metadata of the object 

http Request header

The request header contains a lot of useful information about the client environment and the request body . for example , The request header can declare the language used by the browser , The length of the request body . Example :

Accept:image/gif.image/jpeg./
Accept-Language:zh-cn
Connection:Keep-Alive
Host:localhost
User-Agent:Mozila/4.0(compatible:MSIE5.01:Windows NT5.0)
Accept-Encoding:gzip,deflate.

http Request body

The request body is the body of the request , There can be many kinds of request bodies .

1)json Format

2)xml Format

3)html Format

4) Binary format ( Most are used for pictures )

5) String format

“opt”:”open”

“use”:”yy”

“pwd”:”123456”

http — ordinary http request

https — Encrypted http request , Data transmission is more secure

ftp — File transfer protocol , Mainly used to transfer files

Request protocol :

request IP: It refers to the server address deployed by the system providing the interface

Request port : If you don't fill in the port , The default is 80, Otherwise, you need to fill in the port number

Interface path : It refers to the location of the interface provided by the system

Interface parameters : Parameter after interface path , use “?” To represent the path address , The rest are parameters , use “&” To distinguish the number of parameters ,

The following example :

http://127.0.0.1:8080/light?opt=open&use=yy&pwd=123456

Suppose you want to operate this lamp , User password required , You can add new parameters ”use”、”pwd”, use ”&” To separate . You can see that this example has 3 Parameters :

Interface HTTP Parameters 【url Invisible and other hidden parameters 】

See the request structure above , You can test the interface , You only need to modify the parameters of the interface , You can test it like a functional test . You can use the method of functional test design case to design the use case of interface test . Various test tools can be used to assist in the test , The following figure for “postman” Testing tools .

 picture

 picture

Where will the interface request go

We can look at such an interface , use Java Of Servlet How to achieve

package com.yy.test;

import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

//  Name and path of the interface ,urlPatterns Medium /light, That is, enter the address and ip after , Defined interface name http://127.0.0.1:8080/light 
@WebServlet(name = "light", urlPatterns = "/light")
public class light extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        //  obtain opt Value  
        String opt = request.getParameter("opt");

        //  New return json The object of  
        JSONObject json = new JSONObject();

        //  Judge opt Is it worth it  
        if (null != opt) {
    

            //  Define the returned request header , The type is json, Encoding is utf-8 
            response.setContentType("application/json; charset=UTF-8");

            //  Define the returned object , Use it to json Output  
            PrintWriter out = response.getWriter();

            //  Judge opt Is the value of open 
            if ("open".equals(opt)) {
    
                json.put("msg", " Turn on the light !");
                out.println(json);
            }

            //  Judge opt Is the value of close 
            else if ("close".equals(opt)) {
    
                json.put("msg", " Turn off the lights !");
                out.println(json);
            }
        }
    }
}

The above code roughly does the following operations :

1、 obtain opt Value

2、 Judge what you get opt Value

If it is ”open” Just go back to ” Turn on the light !”

If it is ”close” Just go back to ” Turn off the lights !”

See the code above , You can find , We will opt Value , Spread ”http://127.0.0.1:8080/light“ In this interface , When you get the value you want , You can do whatever you want , Right . The code on my side simply returns , When you visited me “ turn on the light ” The interface of , I'll just tell you “ The lights are on ” ( Actually, I didn't do anything , This is already a BUG 了 . So test the interface to see if it works , in other words , Is the light really on ?), We do interface testing , Interface documents need to be provided for development . The most important points are :

3、 Address of the tested interface

4、 Interface parameters , And the description of each parameter

5、 Necessary http Head and head http body ( http The header can be customized , It can be used to verify whether it is accessed by yourself )

6、 What value does the interface return , And the description of each return value

7、 What is the interface

I've identified these , To do a good job of interface testing . Good interface documents are as follows :

 picture

A more complex interface ,RESTful
 picture

A user interface can be through the following 4 There are two different ways to request , To do different things :

  1. Get user information
  2. Create user
  3. Modify the user
  4. Delete user

You can be like “ The lamp ” The example of , use GET Request to pass different parameters to implement , But if there are more interfaces , It's going to be chaotic , It is difficult to manage .

At this time , We need a rule :

  1. When used “GET” When the way , Just to get data , Successfully returned http Status code 200
  2. When used “POST” When the way , Only used to create data , Successfully returned http Status code 201
  3. When used “PUT” When the way , Only used to modify data , Successfully returned http Status code 203
  4. When used “DELETE” When the way , Only used to delete data , Successfully returned http Status code 204
  5. When the request fails to send , return http Status code 400

Such a rule , Let's call it alpha “RESTful” standard .

The picture below is RESTful The status code of returns

 picture

5. The interface test

The front is clear , Interface testing is simple , It's just a few steps .

  1. Get the interface url Address
  2. See how the interface sends
  3. Add request header , Request body
  4. Send to see the returned results , Check whether the returned result is correct

This is a normal process , Abnormal condition , I don't have to say more. Come on . For example, parameters do not pass values , The value passed is incorrect , It is clearly required to use "GET" Request to send , I have to use "POST" Request to send . Wait, there are many exceptions , Generally, you can think of many exceptions when you understand functional testing , There are no more examples here .

Let me take the interface of Baidu translation as an example :

Request address :http://fanyi.baidu.com/v2transapi

Request mode :POST

Request parameters :

from — Which language

to ---  Translated into which language 

query ---  The translation is all about 

I use Python Language for interface testing , Use ”requests” Library to write code is relatively simple and easy to understand , If not installed ”requests” library , Can open cmd, Input ”pip install requests” To install

import requests

#  Interface url 
url = "http://fanyi.baidu.com/v2transapi"

#  Interface parameters  
params = {
     
    "from":"en", 
    "to":"zh", 
    "query": "test"
}

#  Send interface  
r = requests.request("post", url, params=params)

#  Print return results  
print(r.text)

#  In fact, it's over up there , Because Baidu is not my own interface , In order to see the results more clearly , I got the translated fields  
import json
d = json.loads(r.text)
print(d['liju_result']['tag'])
 Return results 

[' test ', ' test ', ' test ', ' test ', ' Examine ', ' Tested ', ' Tested ', ' Tested ', ' Measured results ']


 Try to put "query" The parameter of is changed to "student", The return result also changes 

import requests

url = "http://fanyi.baidu.com/v2transapi"
params = {
     
    "from":"en", 
    "to":"zh", 
    "query": "student" #  I changed this  
}

r = requests.request("post", url, params=params)

import json
d = json.loads(r.text)
print(d['liju_result']['tag'])
 Return results 

[' middle school student ', ' Student ', ' College students' ', ' researcher ', ' scholars ']

Of course , If you don't understand the code , You can also use tools to test , Just remember to fill in a few points , You can do the interface test , The figure below shows the use of ”postman” To test :

  1. Enter the sending method ”POST”
  2. Enter the test address ”http://fanyi.baidu.com/v2transapi“
  3. Enter the parameters in the red box in the figure below
  4. Click on ”send”, View return results

 picture

Interface testing is that simple

Interface test cases

There is nothing to say about the design of interface test cases , Just design like functional testing . I use Excel To write the , Mainly for the convenience of management , As long as it's clean , Just make people feel comfortable . The use case I designed myself is like this :

  1. One module corresponds to one Excel surface
  2. One interface corresponds to one sheet surface
  3. A row in the table corresponds to a test case
  4. At the beginning of the test, indicate the required sql, Starting from the figure below, I will create a user , I'll delete it when I'm finished

Such use cases are easier to manage , Pictured :

 picture

That's all the interface testing .


Last : Welcome everyone to follow the official account :【 Sad spicy bar 】, Take one 300 page pdf Document Python Summary of the core knowledge of automation test engineer !

Most of the information in the official account is the knowledge point that the interviewer will ask when interviewed , It also includes a lot of common knowledge in the testing industry , It includes basic knowledge 、Linux necessary 、Shell、 The principles of the Internet 、Mysql database 、 Special topic of bag capturing tools 、 Interface testing tool 、 Test advanced -Python Programming 、Web automated testing 、APP automated testing 、 Interface automation testing 、 Testing advanced continuous integration 、 Test architecture development test framework 、 Performance testing 、 Safety test, etc. .

If you have a lot of confusion in the test , Then the software testing technology exchange group I created will be a useful community for you to contact good teachers and friends , Peers may bring you some practical help and breakthrough . Group :902061117 You also want to know how your peers are getting rich !

If it helps you a little , Everyone 「 give the thumbs-up 」 Is the biggest driving force of Xiaobian's creation , See you in the next article !

Good article recommends :

Package as 1 Years working experience as a test engineer , My advice to him before the interview is as follows

interviewer : Work for three years , And a preliminary test ? I'm afraid the title of your software test engineer should be enclosed in double quotation marks …

“ One year in the job , The automated software test hired by high salary was discouraged .”

The man who leaves work on time , Promoted before me …

Summary of automated testing
 Insert picture description here

版权声明
本文为[Test Mengmeng]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/08/20210810081223771h.html

随机推荐