???????monkey patch??????£? python?е?Test Double?? Test Double???????case?и??????????????????. ???????????滻.
??????Test Double??? ??????????????????? Stub??Mock object?? Fake Object?? Mock object ??????????????http://blog.csdn.net/juvxiao/article/details/21562325???????£? ????????????? ???https://wiki.openstack.org/wiki/SmallTestingGuide??? ?????link?л???Test Double???????????: ??????? ?? monkey patching.
???????????
class FamilyTree(object):
def __init__(self?? person_gateway):
self._person_gateway = person_gateway
?????????person_gateway???????????滻?? ????ò???????FamilyTree?????
person_gateway = FakePersonGateway()
# ...
tree = FamilyTree(person_gateway)
monkey patching
??????????????????????python?????????????У? ?????????????滻?????????????????????
class FamilyTree(object):
def __init__(self):
self._person_gateway = mylibrary.dataaccess.PersonGateway()
?????????????????????mylibrary.dataaccess.PersonGateway??????滻?FakeGataway?????.
mylibrary.dataaccess.PersonGateway = FakePersonGateway
# ...
tree = FamilyTree()
??????OpenStack?????monkey patch???????????? ?????????????nova????????test_virt_driver.py?????????monkey patch?÷?
import nova.tests.virt.libvirt.fake_imagebackend as fake_imagebackend
import nova.tests.virt.libvirt.fake_libvirt_utils as fake_libvirt_utils
import nova.tests.virt.libvirt.fakelibvirt as fakelibvirt
sys.modules['libvirt'] = fakelibvirt
import nova.virt.libvirt.driver
import nova.virt.libvirt.firewall
self.useFixture(fixtures.MonkeyPatch(
'nova.virt.libvirt.driver.imagebackend'??
fake_imagebackend))
self.useFixture(fixtures.MonkeyPatch(
'nova.virt.libvirt.driver.libvirt'??
fakelibvirt))
self.useFixture(fixtures.MonkeyPatch(
'nova.virt.libvirt.driver.libvirt_utils'??
fake_libvirt_utils))
??????????????????fixtures module(fixtures?????testcase????? ???Щ???????????????????????????fixtures module?У? ??????ò????????)?????monkey patch?? ????????е?fake object ?????????滻????driver object??????????????????è??????