Abstract: Software quality is an important criterion to measure the success of a software. In the software life cycle, the automated testing pyramid provides you with a testing strategy. According to the specific conditions of the project, the testing activities are optimized, and the software quality is finally obtained. promote.

This article is shared from the HUAWEI CLOUD community " Test Pyramid, which level are you on? ", author: agile little wisdom.

Preface

Software quality is an important criterion for measuring the success of a software. In the software life cycle, if there is no good quality control, it is easy to cause product quality to fail to meet customer expectations and ultimately lead to difficulties in project delivery. Software quality can be guaranteed through standardized R&D processes and systematic software testing. In this article, we will talk about testing-related content.

Test pyramid

Software testing was born along with software development. With the large-scale and complex structure of software, software testing has evolved from simple "debugging" at the beginning to today's automated testing. The original "debugging" will not be discussed in detail here. What is automated testing? Automated testing is a process of transforming human-driven testing behaviors into machine execution. Automated testing usually uses certain tools or frameworks. Although it cannot completely replace manual testing, compared to manual testing, automated testing can reduce labor costs and reduce repetitive work, so that testing activities can be carried out more quickly and efficiently.
image.png

The test pyramid is a pyramid-shaped strategy structure of the automated testing process, which is used to guide the investment ratio of each layer of automated testing in the software development process. It was first proposed by Mike Cohn in the book "Scrum Agile Software Development" in 2009. Mike Cohn pointed out in the book: The testing pyramid is divided into three layers from top to bottom, namely UI testing, service/interface testing, and unit testing. The closer the testing activities are to the bottom of the pyramid, the more work should be invested, that is, unit testing. The workload is the most invested, followed by interface testing, and the least investment in UI testing.
image.png

The bottom of the testing pyramid-unit testing

Unit testing is a code-level test with low writing cost, fast execution speed, and fast location of problems. TDD test-driven development in extreme programming is often carried out around unit testing. Unit testing can be said to be the lowest level of testing activities, and there are a lot of content about unit testing, so I won't introduce too much here.
image.png

Testing the middle layer of the pyramid-interface testing

With the widespread popularity of microservice architecture, APIs have also become the general trend. Therefore, continuous testing of APIs has become one of the concerns of DevOps. Without API interface testing, the implementation of microservice architecture will be a disaster for enterprises.

Interface test is a kind of test to test the interface between the components of the system. It is mainly used to test the interface between the system and other external systems, as well as the interface between the various sub-modules within the system. Interface testing can not only focus on the rationality of the parameter value and parameter value combination of a single interface, but also verify the completeness and correctness of the product function. Compared with unit testing, the coverage of service/interface testing is larger.

The key points of the interface test are as follows:

• Check the correctness of the interface parameter transfer;
• The correctness of the realization of the interface function;
• The correctness of the output results;
• The completeness and rationality of fault-tolerant handling of various abnormal situations.

How to perform interface testing

Swagger is a framework that can generate, describe and call RESTFUL style APIs. The sample Demo of Swagger official website-petstore (pet store) provides a series of interfaces that can add, delete, modify and check pet information. This article uses these interfaces for interface testing.

1. Preparation

First, create test cases for interface testing through the "test cases" in the cloud test function of HUAWEI CLOUD DevCloud.
image.png

Set the URL of the petstore project as the default environment variable, and name it "pethost" here. The test case can directly call this variable in the form of "$$petstore".
image.png

Environment variables can also not be set, but you need to enter the petstore domain name for each test, which is very troublesome. Setting it as an environment variable can reduce the workload. Entering "$$" in the cloud test can directly associate the preset environment variable.
image.png

2. Create an interface test case for "add pet information"

In the petstore project, "add pet information" is completed through a post request, and the request body of the request is as follows
image.png

Create a "URL request", set the request type to "POST", the request address to "$${pethost}/v2/pet", enter the Json string in the above figure in the request body, and the request part is set.
image.png

Next, we design our expected check result. The expected return value is 200, which means success. If another response code is returned, the test fails.
image.png

At the same time, some parameters in the response body must be extracted to facilitate subsequent business testing and use. Here we extract the value of category.id in the corresponding body and assign it to the local variable "id".
image.png

3. Execute the use case and view the return value

After the request is set, we execute the use case, and we can see that the response code is 200, and the result is successful. If you want to see scenarios where the test is unsuccessful, you can try to use other parameters instead of following the parameter list.
image.png

Through the "response" in the "recent results", you can see the return value of this request. The various parameters provided in the return value can be extracted through the "response extraction" function mentioned above for other tests. .
image.png

4. Create an interface test case for "query pet information"

After the creation of the pet information is completed, we use the Get request to query whether the pet is really added.

Create a "URL request", set the request type to "GET", in the request address, directly call the id returned by the previous interface through "$id" (same as "$$", "$" can be directly associated with preset local variables) .
image.png

The response code is set to 200, and the test is expected to pass.
image.png

5. Execute the use case to check whether the pet information can be queried

After executing the use case, you can see that the response code is 200, which is consistent with expectations, and the test is successful.
image.png

The pet information queried by "id" is also consistent with the pet information created before, indicating that these two functions are OK.
image.png

6. Create an interface test case for "delete pet information"

After the test is completed, the test data needs to be deleted.

Create a "URL request", set the request type to "DELETE", and also delete the corresponding pet information through "$id".
image.png

The response code is set to 200, and the test is expected to pass.
image.png

7. Delete test cases

After executing the use case, you can see that the response code is 200, which is consistent with expectations, and the test is successful.
image.png

It can also be seen from the response body that the pet information has been deleted.
image.png

The above is an example of a simple interface test, which shows that the interface test can test the function of a single interface as well as the linkage function of multiple modules of the product.

The highest level of the testing pyramid-UI testing

In the test pyramid, UI testing covers a wide range and is close to the business side, but the cost of writing is high, the execution speed and stability will decrease, and it is difficult to locate problems. Therefore, in the test design, it is necessary to reduce the test of the interface layer. If it is a scenario and logic that can be covered by the lower-level test, in order to increase the speed of the test and save resources, try to put it in the lower-level.

Summarize

For software to have a good quality, rigorous testing activities are indispensable. The automated testing pyramid provides you with a testing strategy. We must optimize the testing activities according to the specific conditions of the project, and ultimately improve the quality of the software.

Recently, HUAWEI CLOUD and colleges and universities jointly launched a school season event, inviting HUAWEI experts and outstanding seniors who won 21offers. It was conducted in the form of online live broadcast + track breakthroughs + generous prizes, aiming to let students understand Huawei's cutting-edge R&D concepts and advanced technologies. Technology, experience The development of scenario applications with different products of HUAWEI CLOUD will deepen college students' understanding of enterprise technology applications, integrate campus learning with enterprise technology applications, and empower college students in careers and employment.

There are a lot of prizes, check it out: HUAWEI CLOUD DevCloud&AI&IoT New Semester Challenge
image.png

Click to follow, and get to know the fresh technology of Huawei Cloud for the first time~


华为云开发者联盟
1.4k 声望1.8k 粉丝

生于云,长于云,让开发者成为决定性力量