???????????????
?????????????mock?????????????????
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from mymodule import rm
import mock
import unittestclass
RmTestCase(unittest.TestCase):
@mock.patch('mymodule.os')
def test_rm(self?? mock_os):
rm("any path")        # test that rm called os.remove with the right parameters
mock_os.remove.assert_called_with("any path")
??????????Щ???????????????????????ò???????з????????????????????????????????????????????????????
????????????
?????????????????????????????mock.patch?????????λ??mymodule.os???????????????????????????????????os???????壬????????mymodule.os??ο????????壿
???????????Python????????????????????÷????????????????????mymodule??????????os??????——????????????Χ?????顣??????????????os?????????????????????mymodule???????
??????仰???????????
???????????????????????????????????????????????????.
?????????????myproject.app.MyElaborateClass???tempfile????????????????myproject.app.tempfile???????????????????????
??????????????????????????
??????’rm’?м??????
??????????? rm ????????? . ??????????????????????????????·???????????????????????????. ????????? rm ???????????:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import os.path
def rm(filename):
if os.path.isfile(filename):
os.remove(filename)
???????. ?????????????????????????????????????????.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from mymodule import rm
import mock
import unittestclass
RmTestCase(unittest.TestCase):
@mock.patch('mymodule.os.path')
@mock.patch('mymodule.os')
def test_rm(self?? mock_os?? mock_path):
# set up the mock
mock_path.isfile.return_value = False
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
rm("any path")
mock_os.remove.assert_called_with("any path")
???????????????????仯??. ?????????????????????????????????????κθ?????.
????????????????????
?????????????????????????????????????????????????????????????????????????????????????????????ζ???????????????????
????????????????rm????????????????????????????????????????????????????????????????????????????????????mock?????????????????????:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import os.path
class RemovalService(object):
"""A service for removing objects from the filesystem."""
def rm(filename):
if os.path.isfile(filename):
os.remove(filename)
????????????????????????????????????????
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from mymodule import RemovalService
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")
????????RemovalService?????????????????????????????????????????????????????????:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import os.path
class RemovalService(object):
"""A service for removing objects from the filesystem."""
def rm(filename):
if os.path.isfile(filename):
os.remove(filename)
class UploadService(object):
def __init__(self?? removal_service):
self.removal_service = removal_service
def upload_complete(filename):
self.removal_service.rm(filename)