我mock
在Python中使用时遇到了一些困难:
def method_under_test():
r = requests.post("http://localhost/post")
print r.ok # prints "<MagicMock name='post().ok' id='11111111'>"
if r.ok:
return StartResult()
else:
raise Exception()
class MethodUnderTestTest(TestCase):
def test_method_under_test(self):
with patch('requests.post') as patched_post:
patched_post.return_value.ok = True
result = method_under_test()
self.assertEqual(type(result), StartResult,
"Failed to return a StartResult.")
测试实际上返回正确的值,但r.ok
它是Mock对象,不是True
。您如何在Pythonmock
库中模拟属性?
print
获得价值,我不会。r.ok
method_under_test
<MagicMock name='post().ok' id='57360464'>
True