1. Why mock
When we encounter the following problems at work, we can use mock to solve them:
1. Unable to control the return of a certain interface of the third-party system, and the returned data does not meet the requirements.
2. Before a certain dependent system is developed, the system under test needs to be tested.
3. Some systems do not support repeated requests or have access frequency limits. If repeated payments are not allowed, the access frequency of the interface for obtaining sensitive information cannot be higher than xx, etc.
2. Implementation principle of mock
In general test scenarios, the return data of the interface is mocked. Take the http protocol as an example,
1. The simplest mock can save a copy of mock data on the mock server and generate a corresponding URL for the mock data. Replace the third-party URL to be accessed with a mock URL, which can meet the most basic mocking needs. You can learn more about this type of mock here.
2. Use proxy to implement mock. Open source tools such as wiremock/anymock provide this implementation. The benefits are:
-
You can view requests and responses to easily obtain prototypes of mock data.
-
Requests that match the rules will return mock data, and requests that do not match the rules access the original service.
-
No need to modify service configuration or restart service
Recently I have come across some function-level mocks. For example, the tool mockito can achieve the function of obtaining mock data without making a request.
Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected. If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up. You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc. Share their experience, and also share many live lectures and technical salons You can learn for free! Focus on it! Open source! ! ! QQ group number: 110685036
3. Benefits of Mock
1. Teams can work in parallel
With Mock, front-end and back-end personnel only need to define the interface document to start working in parallel without affecting each other. They only have close contact in the final joint debugging stage; if there is interface coupling between the back-end and the back-end, it can also be mocked Solution: If the dependent interface is not ready during the test, you can also use Mock; there will be no situation where one team is waiting for another team. In this case, the development self-test phase can be carried out early, so that the time to discover defects is also earlier, which is conducive to ensuring the quality and progress of the entire product.
2. Turn on TDD mode, test-driven development
Unit testing is the cornerstone of TDD implementation, and TDD often encounters situations where collaborative modules have not yet been developed, but with mocks, these are not problems. After the interface is defined, the tester can create a Mock, add the interface to the automated testing environment, and create tests in advance.
3. Can simulate inaccessible resources
For example, if you need to call a resource outside the “wall” to facilitate your own debugging, you can Mock it yourself.
4. Isolation system
If we need to call a post request in order to obtain a certain response to see if the current system can correctly handle the returned “response”, but this post request will cause data pollution in the database, then we can make full use of Mock to construct a virtual For post requests, we can just specify the return for him.
5. Can be used for demonstration
If we need to create a demonstration program and make a simple UI, we can demonstrate it without developing a back-end service at all. Speaking of demonstrations, if you have already built a system and need to demonstrate it to customers, but there is some real data in it that you do not want users to see, then similarly, you can use Mock interfaces to replace all these sensitive information interfaces.
6. Test coverage
If there is an interface with 100 different types of returns, we need to test whether the system can respond normally under different returns. However, some returns will basically not occur under normal circumstances. Do you have to do everything possible to do various things for the system? Hands and feet to get him back for testing? For example, we need to test whether the app crashes when a 500 error occurs in the interface. Don’t tell me that you must do something to the server code to make it return 500. . . With mock, all this is easier. You can simulate whatever return you want. Mom no longer has to worry about my test coverage.
How to mock data
1. Install Node.js and start the local server through http-server
Get the port http://localhost:8000 and then test the data by creating html and json files in the folder
Example:
We can see the output data by opening the browser console.
2. Mock data online
-
Use https://easy-mock.com
-
Use http://rapapi.org/org/index.do
Taking the first one as an example, create a personal project after entering the website
After entering the project, create an interface
Select interface type
Create
View
Console results
4. Processing of mock data in special scenarios
Some scenes are special for mocks. For example, a certain interface has encryption and decryption operations on data, which makes the mock data difficult to intuitively understand and the content of the mock data cannot be adjusted efficiently to complete the test objectives. In addition, some RPC protocols are not universal, and the cost of a universal mock platform to support such protocols is too high. Therefore, we need to make some changes to our approach.
1. For interface encryption and decryption data, we develop a function to support encryption and decryption of mock data. During manual testing, this function can be represented as a plug-in for the mock platform; when the interface is automated, it can be represented as a function that processes mock data. You can also use the method
2. Directly mock the entire logic of this function. Develop a certain function: When the program runs to the specified method, it does not continue to run the function body, but sends an http request to the mock platform to return the mock data.
Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!
Software testing interview document
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 385475 people are learning the system