????OpenStack?е???????mock & mox
???????????? ???????[ 2014/4/2 14:43:06 ] ??????????????? OpenStack
??????????????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()
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11