One article to understand the postman interface test

1. Introduction

Postman is a powerful api debugging and http request tool; it can help test api, provide powerful web api and http request debugging, and can send any type of http request (GET, POST, PUT, UPDATE,,,); and can With parameters and headers.

2. Interface

The api interface refers to the application programming interface (Application Programming Interface), which is some predefined functions, or refers to the agreement for the connection of different components of the software system. A set of routines that applications and developers can access based on a piece of software or hardware without accessing the source code or understanding the details of the inner workings. The essence is function and action.

In layman's terms, it is a service provided by the software to the outside world, mainly for data interaction.

3. Return format

Json format, xml format, HTML format, etc.; mainly json format;
{error_code: code, message: return description, data: return data}

Four, postman use

Create a use case set: After starting Postman, the control panel.

Request is to create a Request test request, and clicking Collection is to create a use case set to save the test request. After the Collection is created, the use case set file rack is generated on the left, and the test interface created each time must be saved in the use case set. Collection is more commonly used. When we maintain many use cases in one or more systems, the first thing we think of is to classify and manage the use cases, and we also hope to do regression testing on these use cases, which is Collection.

5. Interface test

Take creating a get request as an example, usually you need to fill in the url, params, headers, and the params will be automatically spliced ​​to the end of the url. Click the send button, the request is successful, and the response result will be returned.


Postman panel introduction:

Request parameters : params: get method to pass parameters; authorization: authentication, user authentication; headers: request header; body: post method to pass parameters; form_data: form to pass parameters; x-www-from-urlencoeed: pass key-value pairs;

Tests : the assertion after the request; settings: settings.

cookie : The cookie information of the response, used to automatically manage cookie information;

On the far right : document: document description; comment: remarks; Code snippet: export interface automation code, support multiple programming languages ​​(this is the most important, other pieces can be understood); Request detail: request information.

Response page : body: the returned data; pretty: json, xml, HTML: display the returned data in various styles. raw: text format; preview: web page format;

console : The console is mainly used for interface debugging and is very important.

In Tests: write code, extract return information:

6. Interface Association

1. How to deal with interface association?

2. In the interface test, how to deal with multiple interfaces connected in series?

3. How to pass the return value of the previous interface as the parameter of the next interface?

Three ways: the idea is to extract the data first, then save it as a global variable, and extract it for use in the next interface.
1. Extraction by json extractor: convert the data returned by the first interface into json format, extract it, and set it as a global variable; in the second interface, use double braces { {}} to get the value . var responseJson = JSON.parse(responseBody)
2. Regular expression extraction: through regular expression match matching, new regExt sets the rule to obtain, and then sets it as a global variable. The next interface uses double braces { {} } to get the value.
3. Cookie extraction:

Variable setting:
Click Add in the Globals area to add the variables you need;
through the test, first set the return result of the interface as an environment variable, and then you can associate it with the value of the environment variable; call postman.setEnvironmentVariable("id", data) ;Set the environment variable whose variable name is id, and the value is data;

7. Dynamic parameters

1. Built-in dynamic parameters

{ { KaTeX parse error: Expected 'EOF', got '}' at position 10: timestamp}̲} : get timestamp, such as get... randint}} : get a random number from 0-1000, { {$guid}} : get random string of guid

2. Custom dynamic parameters

//手动获得时间戳
var times = Date.now();
//设置为全局变量,
pm.globals.set("times", times);
 
//获取时,需要使用获取全集变量方法;
pm.globals.get("variable_key");

Custom functions: such as interface encryption, interface signature, waiting for a period of time before sending a request, etc.

8. Assertion

Assertion: Judging whether the expected result is consistent with the actual result.

Many methods are provided, and the corresponding code can be generated by double-clicking on the right. For example, status assertion, judging whether the returned status is 200, 404, etc.; business assertion, judging whether the returned result meets expectations.

Nine, test environment variable configuration

The whole process includes development environment, test environment, production environment, etc.; the test addresses ip and ports in different environments are different; therefore, in each test environment, define these ip, ports, etc. as global variables, and then select them according to the global variable name Value; such as: set the ip value of the test environment,

Environment variables: To declare environment variables, first create the environment, and then create variables in the environment. If you want to use environment variables, you must first select (import) this environment, so that you can use the variables in this environment. It should be noted that multiple environments can also be created. Each environment can have multiple variables

insert image description here

insert image description here
If the value is used in the test, directly select the corresponding environment and use global variables; in this way, even the environment variables will not be affected.

Environment variables are also global variables, but they have different functions;

10. Use of the Mock function

Mock is during the test process, for some objects that are not easy to construct, obtain or return, replace it with a virtual object to achieve the desired effect. This virtual object is Mock, which simulates the response content returned by the background to confirm The correctness of the current system.

The usage scenarios are:

1. When the data returned by the system interface does not meet the requirements.
2. The dependent system has not been developed yet, and the system under test needs to be tested.
3. Some systems do not support repeated requests, such as payment functions.
4. System functions have access frequency restrictions, which limit the access frequency of interfaces for obtaining sensitive information.

Take the return of the mock request www.baidu.com page as an example; postman setting steps:

1. Create a new collection; add a request GET: www.baidu.com
2. In the collection, right click and create a new mock server; fill in the mock request and return data; click Next


Name it randomly; click create mock server; if the creation is successful, copy the url, or generate an environment variable for the url,


Initiate the request again; you can see that the response data has changed;


Question: What are the similarities and differences between Cookie, Session and Token?

The same point: they are all returned by the server and used for authentication.

The difference: the cookie is stored on the client side, which is not safe. There is a risk of being intercepted by illegal users; the cookie is stored in the browser's cache, and the expiration time is determined by the server side. As long as the browser is closed, it will become invalid. The session is saved on the server side and passed through cookies. The emergence of Token can solve the problem of cross-domain access in microservice deployment. User login requests go to a special authentication service for authentication. After successful authentication, the encrypted token is returned. After the client gets it, save it, and this will be the next time When the user visits, they will make a request with this token, and the requested service will take this token to the authentication center to see if it belongs to the user, and if it is, then it will be passed and the response result will be returned.

Finally: The complete [Software Testing] learning tutorial below has been sorted out and uploaded, and friends can get it for free if they need it [Guaranteed 100% free]

insert image description here

Guess you like

Origin blog.csdn.net/m0_53918927/article/details/129731128