Ole*_*siy 5 python unit-testing mocking
有什么方法可以使用mock内临时撤消修补side_effect吗?特别是我想做这样的工作:
from mock import patch
import urllib2
import unittest
class SimpleTest(unittest.TestCase):
def setUp(self):
self.urlpatcher = patch('urllib2.urlopen')
self.urlopen = self.urlpatcher.start()
def side_effect(url):
#do some interesting stuff first
#... temporary unpatch urllib2.urlopen so that we can call a real one here
r = urllib2.urlopen(url) #this ought to be the real deal now
#... patch it again
return r
self.urlopen.side_effect = side_effect
def test_feature(self):
#almost real urllib2.urlopen usage goes here
p = urllib2.urlopen("www.google.com").read()
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
小智 3
为什么不暂时.stop()打补丁呢?
self.urlopen.stop()
# points to the real `urlopen()` now
urllib2.urlopen()
# put the patch in place again
self.urlopen.start()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2607 次 |
| 最近记录: |