This article is participating in Python Theme Month. See the link to the event for more details
The mock definition
- What is a Mock test?
A mock test is a test method that is created using a virtual object to test an object that is not easy to construct or retrieve during testing.Copy the code
- What if I don’t mock?
Personally, I believe that in the era of rapid development of the Internet and the need to respond quickly to the market, the product must be updated and iteratively quickly if it wants to live and gain more users. In the Internet company, the general convention of a half a month iteration, fast is a few days iteration, if the development mode, is the full stack development engineer, that is the front and back end of a person, not afraid of < death > to come; I don't know if this is good or bad, in short, time is indispensable, and testing is indispensable; The problem is that the front end development is separated and the task is broken down after the requirements review!!Copy the code
-
- 1. The students at the back end began to develop the interface, and the front end got the UI design draft and began to layout the screen, but the interface did not come out?
Excuse me? Front end to watch the back end of the students? Urging every day, out of the interface, waiting for the tune? You deserve to work overtime!! So this intermediate time difference is a loss, and finally wait for the completion of the back-end development to start the test, then the time left for the test students is not much, the cycle is tight, the quality is also worried?Copy the code
-
- 2. Case 2: Backend A must rely on the interface developed by backend B student, so is he waiting?
Do you think iteration time is too long, want to work overtime?Copy the code
-
- 3, Case 3: About the third party service, if it is a performance scenario, you can’t go to the other server, then how to own test?
What should you do when you don't need to pay special attention to third-party business? No, the iteration cycle is much shorter than you think, QA/ project manager/technical manager/operations/customer are all after the project, don't wait to be hunted down, it will affect the quality of the project and make people hate it.Copy the code
- How do you solve this problem?
So, the meaning of this mock came out, the front is no longer dependent on the back-end, the backend is no longer dependent on back-end B, but parallel development, such as reach A point in time all tasks to complete the development and integration test again > < front end alignment test, at least at the end of the plot to mention earlier, although not absolutely early; But it's also a good collaborative office model. How can we not rely on it? I don't think there's a better way to work than to review the requirements, validate the requirements, identify the work that needs to be done, agree on the front and back data structures, document the interface, and then fire.Copy the code
- Having said the definition and the problem and the solution, let’s look at the practical application
Unittest testing framework
- The author, who is comfortable with Python, uses the standard unittest framework as an example
Basic knowledge of Python is required; Mock has a separate library in python2, and python3 is directly integrated into the unittest framework.Copy the code
- Scenario 1: Interface B depends on interface A, so interface B can request interface B only if interface A requests interface B properly
class Demo() :
def pay(self) :
""" this is a payment interface developed by Student B, which is not yet completed; {" MSG ":"failed","code": 0,"reason":None} {" MSG ":"failed","code":-1,"reason":" payment account or password error "} Return failure cause """
pass
def get_status(self) :
""" According to payment result status """
res=self.pay()
try:
if res["msg"] = ="success":
return "Successful payment"
elif res["msg"] = ="failed":
return "Payment failure"
else:
return "Unknown exception"
except:
return "Error, server exception"
Copy the code
- Design unit test cases according to Demo module
import unittest
from unittest import mock
class TestPayStatus(unittest.TestCase) :
Unit test case
# Instantiate an object
demo=Demo()
def test_success(self) :
""" test payment success scenario """
# Simulated payment successful response
self.demo.pay=mock.Mock(return_value={"msg":"success"."code":0."reason":None})
status=self.demo.get_status()
# assertion
self.assertEqual(status, "Successful payment")
def test_failed(self) :
""" "Test payment failure scenario """
# Simulate payment failure response
self.demo.pay=mock.Mock(return_value={"msg":"failed"."code": -1."reason":"Payment account or password is incorrect"})
status=self.demo.get_status()
# assertion
self.assertEqual(status, "Payment failure")
def test_error(self) :
""" Test the exception scenario """
# Simulate payment failure response
self.demo.pay=mock.Mock(return_value={"msg":"error"."code": -2."reason":"Unknown exception"})
status=self.demo.get_status()
# assertion
self.assertEqual(status, "Unknown exception")
def test_error2(self) :
""" Test server exception scenario """
# Simulate payment failure response
self.demo.pay=mock.Mock(return_value={})
status=self.demo.get_status()
# assertion
self.assertEqual(status, "Error, server exception")
Copy the code
- The above case, is not very simple, and use case coverage reached 100%, welcome big guy clap brick!!