Jmeter implements an interface business closed-loop test script example

The interviewer asks like this:

Can you give an example of interface automation testing that actually happened in your work?

Reference answer:

My previous company’s business was XXX, and the project form was to continuously iterate new requirements on large-scale stable projects. Therefore, every time I go online, in addition to ensuring the correctness of new requirements, I also need to ensure that the main process of the original system is smooth. Therefore, a fixed regression test must be done every time it goes online. In the beginning, everyone spent about an hour manually clicking the main process. Later, I saw that the repetitive work was too heavy, so I made a Jmeter version of the interface automation, such as using the logic controller that comes with Jmeter, putting the login interface in the only one-time controller, and using regular expressions or Json to extract After the server obtains the Token value, the request headers of all subsequent business interfaces will carry this Token value, and then realize the business closed-loop scenario of adding, checking, modifying, and deleting in the background management system. Because the automation scenario is often run, it must be It is necessary to make the business closed loop, otherwise, if only new additions are made, a large amount of test data will accumulate, causing unnecessary pressure on the server.

Then when I do new operations, I usually use the loop controller, add multiple pieces of data at the same time, query the interface to query a list, and then use counters and timers when doing loops such as modifying and deleting operations (analog thinking time) to assist.

After designing the script, after the local connection, I will upload the script to the server, and then use the Jenkins tool to set up regular execution every day. If there is any problem, I will send an email to me, so that I can find the problem in time and save money. A lot of time for manual regression testing.

JMeter specific implementation

I won’t say much about basic operations such as building test plans and thread groups. There are a few points to note:

1. HTTP header management:

As shown in the figure below, the general interface document indicates that if the required request header contains Content-Type, it must be added, otherwise the interface request cannot be made. Then token is a token token. From the perspective of security, it must be authorized through login. After obtaining the token token, other interfaces can continue to access by passing the token value in the request header, so add it to the request header. The method of using parameter references (extracted with regular expressions or Json extractors in the login interface)

 

2. HTTP request default value

In order not to write the ip address once for each interface in the future, it can be solved globally at one time

 

3. The login interface and the one-time controller

In general, for interface automatic scenarios, unless the login interface is specially tested, it is generally recommended to put it in the controller for only one time, and only need to log in once, and the subsequent interfaces will not be affected no matter how they cycle. The main purpose of this login interface is to obtain system authorization.

 

As shown in the figure below, for the sake of security, the token obtained every time you log in will change, so it cannot be hard-coded, you can only extract its value every time, and pass it to the HTTP request header as a global parameter.

 

This JSON extractor is not required to extract. It is only because of the IF controller used below that it is judged that when the login is successful, it runs the following interface. So in order to judge the success of the login, you can use the JSON extractor to extract the code return value.

 

4. IF controller

If we need to do different operations on different conditions, we can use the (if) controller to achieve

As shown in the figure below, use the function assistant to refer to the login interface to obtain the login_code, and use equal to 200 as the judgment condition, click Generate and paste it into the IF controller. If it is not equal to 200, all interfaces in the entire controller will not run.

 

 

5. New interface

 

random function:

 

6. Query interface

Because the new addition is to add multiple new ones in a loop, a list can be found by querying by name keywords, and then all rows in the list can be edited and deleted to complete the business closed loop. Then the query does not need a loop controller, it only needs to be checked once

 

7. Debug the sampler:

Adding a debug sampler after the query interface is to obtain the detected id and zoneName information:

 

For modification and deletion, you can use the loop controller and the ForEach controller. The effect is the same, but the method is different. Write one separately.

8. Loop Controller and Modification

First of all, the number of cycles is not hard-coded, it needs to be determined according to the length of the queried list

 

In order to edit the records differently each time, and change them one by one from the beginning to the end, a counter needs to be added

 

modify interface

 

Concatenate multiple parameters with the help of function helpers

 

9. ForEach controller and delete

 

 

Guess you like

Origin blog.csdn.net/shuirongwu/article/details/129478961