??????????????OpenStackcode?????????????д????????????UT???????????mock & mox ????????????????????飬 ?????????????????UT?????
????mock & mox:
????????python????????????????module?? ????????? ??????滻?????????е??????(????class?? function??object).  ????function??????????????????. ?????db?????? I/O?? ????????????socket?? ssh??????? ????????й????У? ????е???????? ?????????????????????У? ????????????????????????
????mock??????????????mox??????????? mock??'action->assertion' ????? mox??????'record -> replay->verify'????? ????????????
????mock :
????????????????????mock???????????y?????
????>>> from mock import Mock
????>>> real = ProductionClass()
????>>> real.method = Mock(return_value=3)
????>>> real.method(3?? 4?? 5?? key='value')
????3
????>>> real.method.assert_called_with(3?? 4?? 5?? key='value')
??????????action?? ?????assertion????????????? ?????????????????????????.
????mox:
????mox ?????????????????????mox ???е?package?п?????mox.py?п???????????????? ???????????????????????? ????????????????????????????????????
????record mode: ???????mock???? ????mock?????????????? ?緵?????? ??????????????.
????replay mode: ?????????????????? ??????????????
????verify mode: ????????У????????recode mode??????Щ??????????????У? ?????????.
????????mox????mock object???????????????? ??????????μ??????????????.
??????????????mox.py?? ???????????????mox????????? 3-13???recode mode?? 14-19???replay mode?? 19??????verify mode. ????????ù????к??????????? ?????mox??????????????. ????????
Suggested usage / workflow:
# Create Mox factory
my_mox = Mox()
# Create a mock data access object
mock_dao = my_mox.CreateMock(DAOClass)
# Set up expected behavior
mock_dao.RetrievePersonWithIdentifier('1').AndReturn(person)
mock_dao.DeletePerson(person)
# Put mocks in replay mode
my_mox.ReplayAll()
# Inject mock object and run test
controller.SetDao(mock_dao)
controller.DeletePersonById('1')
# Verify all methods were called as expected
my_mox.VerifyAll()