A concept .Mock
unittest.mock is a library for unit testing in Python, Mock translates analog means, as the name suggests the main feature of this library is to simulate something.
Its main function is to replace the use of mock objects out of the specified Python objects, simulate the behavior in order to achieve the object.
The role of two .Mock
1. If you're writing an interface automation, and then returns the results to the interface needs A B interface you use, then you can use a Mock.
2. dependent on third-party interfaces, making the interface automation of time, sometimes need to call a third-party interface, but other people's interface services beyond your control, there may be others provide testing services to today's environment you are driving, others it is turned off,
to the automation interface testing a lot of trouble, then you can write your own mock-server interface to simulate the return data.
Import of three .Mock
Mock in py3.0 after unittest framework has been put inside, so direct from unittest import mock on the line
IV. Examples of presentation
mock_data.py
. 1 DEF Register (): 2 '' ' The assumption is a registered function, incompletely developed three successful registration returns: {' status': 1, 'code': '10001', 'data': None, 'msg' : "registration is successful"} 4 registration failed returns: { 'status': 0, ' code ':' 20110 ',' data ': None,' msg ':' phone number is already registered '} . 5 ' '' . 6 Pass . 7 DEF register_statues (): . 8 '' ' is determined registration is successful ' '' . 9 Result = Register () 10 Print (Result) . 11 iF Result [ " code "] == "10001": 12 return " Registration successful " 13 elif the Result [ " code " ] == " 20110 " : 14 return " phone number has been registered ," 15 the else : 16 return " Unknown error exception ."
test_demo.py
. 1 from the unittest Import the mock 2 Import the unittest . 3 from test_case_demo Import mock_data . 4 . 5 class Test_Register (of unittest.TestCase): . 6 '' ' unit test ' '' . 7 DEF test_01 (Self): . 8 '' ' test scenario registration success ' ' . 9 # the mock a successful data register 10 mock_data.register = mock.Mock (the return_value = { ' Status ' :. 1, ' code ' : '10001', ' Data ' : None, ' MSG ' : ' registered successfully ' }) 11 # obtain the results according to the registration 12 is Statues = mock_data.register_statues () 13 is # Print (Statues) 14 self.assertEqual (Statues, " registration is successful " ) 15 16 DEF test_02 (Self): . 17 '' ' test scenario registration failure ' '' 18 is # the mock data of a registration failure . 19 mock_data.register = mock.Mock (the return_value = { ' Status ': 0, ' code ' : ' 20110 ' , ' Data ' : None, ' MSG ' : ' phone number is already registered ' }) 20 # obtain the results according to the registration 21 is Statues = mock_data.register_statues () 22 is # Print (Statues) 23 self.assertEqual (Statues, " the phone number has been registered " ) 24- 25 IF __name__ == " __main__ " : 26 unittest.main ()
result: