4
头图

Why do interface automation

Compared with UI automation, interface automation has greater value.

In order to optimize the conversion path or improve the user experience, the button controls and layout of the APP/web interface are changed almost every version, which leads to frequent changes of the automated code, which does not reduce the workload.

Once the interface development is completed, the frequency of subsequent reconstruction/substantial modification is relatively low. Therefore, the cost-effectiveness of interface automation is still very high. For the regression of the iterative version of the old functions, beta testing, and online regression can achieve twice the result with half the effort. effect.

This article does not discuss the testing of a single interface in detail. Let's mainly analyze how to do interface automation based on business scenarios.

Where is the problem

A business scenario usually requires multiple interfaces to complete a complete business process, where each interface completes a specific functional step. For example, the process of adding friends on WeChat:

There is at least one interface request in each operation step. Then we express this process in the form of interface request, as follows:

仅作示意,并非实际

We want to complete this interface use case, what are the required operations?

1) Enter the WeChat id and send a query request
2) Pass the obtained user information to the "add friend interface" to initiate a request to add a friend
3) Pass the user information of the requesting friend to the send friend request interface
4) Pass the consent information to the become friend message interface
5) Pass the rejection message to the rejection message interface
summary, there are several problems that need to be solved: test data transfer, interface dependency, and parameter transfer between interfaces.
This is also a common problem encountered in interface test automation, and the solution can be migrated to various businesses. Next, the author will propose some specific solutions to the above problems

tool introduction interface testing tool used in this article: Apifox Windows version
As an interface testing tool, Postman's position in the industry is unquestionable, but Apifox, as a local interface debugging, interface testing & test management software, has the advantage of conforming to the testing mode and workflow of the domestic Internet, making it easier to use.
Most of the functions can be completed by the visual interface + control operation. For testers who do not understand code or write scripts, the task of interface automation can be completed smoothly and painlessly.
Therefore, this article will directly use Apifox when explaining the automation. If you need to follow the article to explain and learn, you can first go to the official website (www.apifox.cn) to download and register one. The software is free.

All by parameterization

The advantage of manual testing is that it is flexible and controllable, while automation relies on our pre-set steps to complete the function.

Parameter transfer between interfaces

Like the above example of WeChat friend request, it involves parameter transfer between multiple interfaces. The next interface pair depends on a field in the response of the previous interface, and it needs to be accurately extracted and passed.

solution : extract the response data of the previous interface-parameterization-the next interface calls the parameter.
In this way, no matter how many times the interface runs or how the passed parameters change, the next interface can be dynamically extracted and called.
operation steps on 161d5c08fbb902 Apifox are as follows:
1) In the post step of the interface to extract the parameters, use the json path expression to extract the target response field and name it, and set the variable type

2) This field will be saved in the project settings. In the same environment, other interfaces in the same project have the calling authority.
Run the interface in the above figure, you can see that the field has been extracted into the variable setting.

3) In the interface request parameters that need to call the variable as a parameter, fill in the corresponding space in the form of a parameter

look at the result:
Send the post request, you can see in Interface>Run>Actual request tab>Request URL that the parameter has been successfully called

Test data parameterization

  1. uses variable
    Some test data (such as login account password, user information, etc.) will be called repeatedly in different interfaces. At this time, the test data can be parameterized. Unlike the interface response parameters, the response parameters are obtained from the previous interface. , And the test parameters are written directly into the variable settings.

    in Apifox are as follows:

    1) Set the test data variable name and value in the global variable

    2) Call the test data directly in the interface request parameters

  2. uses the test data set
    When we need to upload different test data to verify whether the response data is abnormal, one interface parameter requires multiple test data. We put this into the part of test management 161d5c08fbbc11.

Test assertion

Since it is an automated test, we cannot manually verify whether the returned response meets our requirements, so we need to use scripts/function settings to complete these tasks for humans.
We mainly check:
*1) Whether the interface request is successful, that is, whether the returned code meets expectations
2) Whether the key fields and key values in the returned interface data meet expectations*

On Apifox , basic assertion operations can be set directly through interface operations:
1) Select assertion in interface management-post operation

2) After the request is sent, if the return value meets expectations, the return will prompt the assertion to succeed, and if it fails, an error prompt will be given.


If you need to set the assertion flexibly, you can use the custom script function in the post-operation in apifox. It also provides a code template function, and the tester only needs to modify the expected value.

For a single interface, the automated preparatory work is to parameterize the input data and the parameter transfer between the interfaces, do not write hard, modify and maintain the later data, and use test assertions instead of manually checking the interface test results.

After completing this part of the work, we can then organize the different interfaces into an interface automation use case to complete the test of a business scenario.

The next job we Apifox of test management the Tab completed.

Test management

Import test cases

Interface use cases need to be generated when testing a single interface. This is the preparatory work that needs to be completed before importing test cases. When testing a single interface, select save it as a use case to generate a test case.

In the test management tab, Create a new test case>Import interface case>Select the required use case of the test scenario>Select binding by default>OK to import

The use case after the import is as follows, an interface request is an operation step, and several steps jointly complete the test of a scene.

Interface execution order

In the above use case, the steps of the interface request are from top to bottom. If you want to adjust the running sequence of the interface, you can drag the interface directly to the target location.
If you need to add other interface requests, select add step .

Use test data set

In the section of test data parameterization mentioned above, one interface requires multiple test data. It is mainly because the function module is just here, which is more convenient.
On the right side of Test Management>Use Cases, you can see the function of test data

Click Manage data, you can enter test data tab management to add external test data.


Then in the test step> settings page, modify the test data to the variable name of the test data set


When you click the run button below, an interface will pop up for the tester to select the test data set to be used, and each test data set will be run as a test case.


Correspondingly, multiple test results will be generated:

In addition to being able to fill in directly, you can also import an external CSV file, which is more suitable for test scenarios with large data volumes.

Test parameter configuration

On the right side of the use case, there are configurations such as running environment and saving variable values, which can be selected according to the actual needs of the project.
Among them, [Interval pause] and [Use global cookie] are used more frequently in interface testing.

Run result & test report

Click the [Run] button, and the successful operation will jump to the operation result page. You can also export test reports.

Test suite

The interface use cases of the same module can be combined into a test suite to run. On the test suite page, add a single interface use case directly. The other operation steps are the same as above.
Click Run to run the added use cases in sequence.

Summarize

After the overall explanation, I feel that the advantage of interface automation is that the 161d5c08fbbfba process is highly integrated with the , low code , 161d5c08fbbfb, and the working mode and thinking mode of the domestic testing team .

Therefore, from single interface testing, to business process interface testing, to the entire test suite assembly, and automating them, all the way down, the next step can reuse the work results of the previous step, and there is almost no wasted work. quantity.

Another point is that we use Apifox to achieve automation, but there is almost no need to use code in the process. Many places are directly made into a visual interface by it. We just choose the control to fill in the data. This is a weak code foundation. The testers are indeed a great boon.

This time, "Apifox's Interface Automation Test Strategy" has been explained to you, and I hope it will be helpful to you.


默默
38 声望3 粉丝