???????壺??????
????????????????????????????????????????????????????????????????????????????????????????? ??????????????????? ?????????????
????@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 ???????? ?????μ????????????????????????????????????α????
????patch_sys(patch_os(patch_os_path(test_something)))
??????? sys ????λ???????????????У????????????????????????????????????????????????????????????????????????????????????????????????????????
???????? 2?????? Mock ???
?????????????ù?????? UploadService ????? Mock ?????????????????????????????????????? 1??????????????????????????????? 2 ?????????Ч????????????????????????????
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from mymodule import RemovalService?? UploadService
import mock
import unittest
class 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")
??????????????У??????????????????κι???????? RemovalService ??????? auto-spec???????????????? 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 value
????def 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 facebook
????class 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)
????????????????????????????????????????????????????????????????
????import facebook
????import simple_facebook
????import mock
????import unittest
????class 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 ???
??????????????????е????????? ??????? ?????Python ?? mock ???????????????????????????????????????????????? mock ?????????е?????????????????????? Python ?????? ?????????????д?????????????????