?????????????????????????????RemovalService?? ?????????????????????UploadService?????????rm??????????????????????UploadService??RemovalService.rm?????????м?????????????????????????????????????????????????????????????????????
????????????????????????????:
????1.???RemovalService.rm?????????
????2.??UploadService???????????????????????
???????????????????????????з?????????????????????????????????????л???
???????1: ???????????
???????????????????????????????????????????????????@mock.patch.object ??????Σ?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from mymodule import RemovalService?? UploadService
import mock
import unittestclass
RemovalServiceTestCase(unittest.TestCase):
@mock.patch('mymodule.os.path')
@mock.patch('mymodule.os')
def test_rm(self?? mock_os?? mock_path):
# instantiate our service
reference = RemovalService()
# set up the mock
mock_path.isfile.return_value = False
reference.rm("any path")
# test that the remove call was NOT called.
self.assertFalse(mock_os.remove.called?? "Failed to not remove the file if not present.")
# make the file 'exist'
mock_path.isfile.return_value = True
reference.rm("any path")
mock_os.remove.assert_called_with("any path")
class UploadServiceTestCase(unittest.TestCase):
@mock.patch.object(RemovalService?? 'rm')
def test_upload_complete(self?? mock_rm):
# build our dependencies
removal_service = RemovalService()
reference = UploadService(removal_service)
# call upload_complete?? which should?? in turn?? call `rm`:
reference.upload_complete("my uploaded file")
# check that it called the rm method of any RemovalService
mock_rm.assert_called_with("my uploaded file")
# check that it called the rm method of _our_ removal_service
removal_service.rm.assert_called_with("my uploaded file")
???????????????????????????????????????rm??????????????????????????????????????????????????????????????????????????????rm??????????ζ??????????????????????????????????????????????????????????????????????????????????????????ι??????
???????壺??ε????
?????????????η???????β?????????????ε?????????????????????????????????η?????????????????????????????η??????????????????????????????????
@mock.patch('mymodule.sys')
@mock.patch('mymodule.os')
@mock.patch('mymodule.os.path')
def test_something(self?? mock_os_path?? mock_os?? mock_sys):
pass
??????????????????η?????????????????? ?????в???????????Python???????????????????????η?????????????????????
????1
????patch_sys(patch_os(patch_os_path(test_something)))
???????????????sys???????????????????У???????????????????????????????????????????????????????????????????????????????????????????
???????2: ????????????
?????????????UploadService???????????????????????????????????????????????????? ???????????1??????????????????????????????£????2??????????????Ч???????????????????????????
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from mymodule import RemovalService?? UploadService
import mock
import unittestclass
RemovalServiceTestCase(unittest.TestCase):
@mock.patch('mymodule.os.path')
@mock.patch('mymodule.os')
def test_rm(self?? mock_os?? mock_path):
# instantiate our service
reference = RemovalService()
# set up the mock
mock_path.isfile.return_value = False
reference.rm("any path")
# test that the remove call was NOT called.
self.assertFalse(mock_os.remove.called?? "Failed to not remove the file if not present.")
# make the file 'exist'
mock_path.isfile.return_value = True
reference.rm("any path")
mock_os.remove.assert_called_with("any path")
class UploadServiceTestCase(unittest.TestCase):
def test_upload_complete(self?? mock_rm):
# build our dependencies
mock_removal_service = mock.create_autospec(RemovalService)
reference = UploadService(mock_removal_service)
# call upload_complete?? which should?? in turn?? call `rm`:
reference.upload_complete("my uploaded file")
# test that it called the rm method
mock_removal_service.rm.assert_called_with("my uploaded file")
??????????????У???????????????????κι??????貼???????auto-spec??????RemovalService?????????????UploadService?ж?????????
????mock.create_autospec?????????????????????????ζ?????????????????÷??????????н????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????仯??????ж?????????????????????????auto-spec??????????????????????????????????????
???????壺mock.Mock??mock.MagicMock??
????mock????????????????mock.Mock??mock.MagicMock????????????????????????????????????????????mock.Mock?????mock.MagicMock?????auto-spec???????????????????????? auto-spec????????????????δ????仯?????????????????????mock.Mock??mock.MagicMock?????????API?????????е???????ú??????????????????????????
class Target(object):
def apply(value):
return valuedef method(target?? value):
return target.apply(value)
?????????????????????mock.Mock????????????
class MethodTestCase(unittest.TestCase):
def test_method(self):
target = mock.Mock()
method(target?? "value")
target.apply.assert_called_with("value")
??????????????????????????????Target.apply????????????????
class Target(object):
def apply(value?? are_you_sure):
if are_you_sure:
return value
else:
return None
???????????????????????????????????????????????????????????API??????????????????????????create_autospec???????????????@patch??@patch.object??η???????autospec??????
????????????????: ?????? Facebook API ????
?????????????????д???????????????????????? ???????????????????????: ??Facebook??????????. ?????д?????????????????????????????????.
import facebookclass
SimpleFacebook(object):
def __init__(self?? oauth_token):
self.graph = facebook.GraphAPI(oauth_token)    def post_message(self?? message):
"""Posts a message to the Facebook wall."""
self.graph.put_object("me"?? "feed"?? message=message)
???????????????????????? ????鵽??????????????????????????????????????Facebook???:
import facebook
import simple_facebook
import mock
import unittestclass
SimpleFacebookTestCase(unittest.TestCase):
@mock.patch.object(facebook.GraphAPI?? 'put_object'?? autospec=True)
def test_post_message(self?? mock_put_object):
sf = simple_facebook.SimpleFacebook("fake oauth token")
sf.post_message("Hello World!")        # verify
mock_put_object.assert_called_with(message="Hello World!")
?????????????????????Python???? mock ?????д?????????????????????.
???????
????Python?? mock ?? ??????????е?????? ????????????????????. ??????????????????????? mock ???????Щ???????ó????? ??????????????? Python ??????????????д???????????????????.