"Blast Liver Tidying" Nanny Series Tutorial - Playing with Charles Packet Grabbing Artifact Tutorial (13) - How Charles Mock and Interface Test

1 Introduction

The biggest advantage of Charles is packet capture analysis, and most of the functions we use are also packet capture functions, but don't forget that Charles can also do interface testing. As for Mock, it has already been introduced in modifying the request and response data. Brother Hong is here to briefly introduce its theoretical knowledge. Today, the main introduction and sharing is the practical operation of interface testing using Charles.

2. What is Mock?

Baidu Encyclopedia says this: Mock testing is a test method that uses a virtual object to create a test method for some objects that are not easy to construct or obtain during the test process.

Mock is to create a Mock object to simulate the behavior of the object for some objects that are not easy to construct/obtain during the test process.

The Mock method is a common technique in unit testing. Its main function is to simulate some objects that are not easy to construct or are more complicated in the application, so as to isolate the test from the objects outside the test boundary.

If you want to learn interface automation testing, here I recommend a set of videos for you. This video can be said to be the number one interface automation testing tutorial on the entire network at station B. At the same time, the number of online users has reached 1,000, and there are notes to collect and use. Technical exchanges of various masters: 798478386    

[Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (the latest version of actual combat)_哔哩哔哩_bilibili [Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (actual combat) The latest version) has a total of 200 videos, including: 1. [Interface Automation] The current market situation of software testing and the ability standards of testers. , 2. [Interface Automation] Fully skilled in the Requests library and the underlying method call logic, 3. [Interface Automation] interface automation combat and the application of regular expressions and JsonPath extractors, etc. For more exciting videos, please pay attention to the UP account. https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337&vd_source=488d25e59e6c5b111f7a1a1a16ecbe9a

3. Advantages of Mock testing

3.1 Team work in parallel

During the development process, as long as the interaction parties define the interface, the teams can work in parallel, the process does not affect each other, and there is no need to wait for each other's progress. Just agree on the data specification (interface document) between each other, and then use the mock to build Find out the available interface, and then develop and self-test as soon as possible to find defects in advance.

3.2 Test-Driven Development TDD (Test-Driven Development)

Unit testing is the cornerstone of TDD implementation, and TDD often encounters the situation that the collaborative module has not been developed yet, but with mock, testers can access the test in advance to improve test efficiency. After the interface is defined, the tester can create A Mock, add the interface to the automated test environment, and create tests in advance.

3.3 Test coverage

The interface involves input parameters, or the business logic is complex, some scenarios cannot be operated through normal means, but through mock virtual simulation, the coverage can be effectively increased

3.4 Isolation system

If an interface returns different values ​​in different states, the common practice is to reproduce this state and then request the interface. However, when using some interfaces, it may fail due to improper operation timing or method, and even pollute the backend storage Such as database, etc., in order to avoid contamination of the system database, the interface can be adjusted to Mock mode to ensure the purity of the database.

4. Disadvantages of Mock testing

Mock is not a panacea, and there are risks in using Mock. It is necessary to determine whether to use Mock according to the actual situation and specific needs of the project.

If a large number of mocks are used during the test, the scene of the mock test will lose its authenticity, which may lead to the discovery of bugs in subsequent systematic tests, making the discovery of defects late, which may result in higher subsequent repair costs

5. Mock client

The interface document has been docked, but the back-end interface has not been developed yet. Testers need to conduct unit tests, or conduct interface automation tests in advance, and need to use the content returned by Mock various requests.

6. Mock server

When the front-end personnel have completed the development tasks, but the back-end personnel have not yet completed the interface development, and need to jointly debug and test with the front-end personnel, a Mock service needs to be built.

Well, the theory of Mock is introduced here, and those who are interested can check the information privately. The next step is to enter today's focus: using Charles for interface testing.

7.Compose

For testing with Charles, we mainly use Composer functionality. This is similar to the Fiddler tool. Composer allows custom requests to be sent to the server, and a new request can be created manually. The specific operation steps are as follows:

7.1 Method 1

1. Open Charles, click [Tools] --> [Compose] or [Compose New...]. As shown below:

2. Fill in the information of the interface you want to test. If there is an interface document, fill in according to the interface document. If not, fill in the packet by yourself, as shown in the figure below:

 

7.2 Method 2

1. Select the captured interface request information, right click --> [Compose], as shown in the figure below:

 2. The relevant information of the interface request appears. As shown below: 

 

 

8. Interface function analysis

Describe the interface functions of Composer according to the position marked in the figure below.

1. Request method: click to select the request protocol is get, post, etc.

2.url address bar: enter the requested url address

3. After the Execute button is clicked, the request can be executed

4. After the execution is completed, a history record will be generated in the History area on the right

5. http version: You can check the http version

6. Add request parameters

9. Interface test practice

9.1 POST API combat

Now the free interface is very hard to find. After searching for a long time, I found one in python, which was also used in Fiddler before. Just use this as an example.

Interface information:

interface information describe
https://httpbin.org/post interface address
request method HTTP POST
request parameters Request parameter format is JSON

 Parameter format:

{
    "form": {
        "comments": "测试猿廖廖post接口测试", 
        "custemail": "[email protected]", 
        "custname": "测试猿廖廖", 
        "custtel": "13045032408", 
        "delivery": "15:45"
  }
}

So how to use Charles to debug a POST request with data in json format, the specific steps are as follows:

1. Start Charles, open the Compose panel, enter the URL in the Compose new panel (enter the address of the request interface), and select the POST request method in the drop-down box, as shown in the following figure:

 

2. After clicking [Compose], as shown in the figure below:

 3. Click [JSON Text], then enter the requested JSON data, and finally click [Execute] as shown below:

 4. Click Execute, if you see the result in the session list and return 200, it means the operation is successful, as shown in the figure below:

5. Check the result of the request response in the Inspectors. We know in advance that the interface returns data in json format. Click the json in the response part to format the data for easy reading. As shown below:

 

9.2 GET API combat

Interface information: a get request with parameters, such as searching on Douban: Journey to the West, the url address is: https://www.douban.com/search?q=Journey to the West. The specific operation is shown in the figure below:

1. Start Charles, open the Compose panel, enter the URL in the Composer panel, and select the GET request method in the drop-down box, as shown in the following figure:

2. Enter the parameters of the request interface, as shown in the figure below: 

 

 3. Click Execute, if you see the result in the session list and return 200, it means the operation is successful, as shown in the figure below:

 

10. Summary

 Today, I mainly explained and shared two request methods on how to use Charles for interface testing. Other requests are similar! ! ! Well, it’s not too early today, Brother Hong will explain and share here, thank you for reading patiently! ! !

Guess you like

Origin blog.csdn.net/m0_73409141/article/details/131898712