python mock数据_Python Mock中的Mock属性?

模拟版本“1.0.1”支持问题中提到的更简单的语法,并按原样工作!

示例代码已更新(使用py.test而不是unittest):import mock

import requests

def method_under_test():

r = requests.post("http://localhost/post")

print r.ok

if r.ok:

return r.ok

else:

raise Exception()

def test_method_under_test():

with mock.patch('requests.post') as patched_post:

patched_post.return_value.ok = True

result = method_under_test()

assert result is True, "mock ok failed"

运行此代码时使用:(确保安装了pytest)$ py.test -s -v mock_attributes.py

======= test session starts =======================

platform linux2 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2 -- /home/developer/miniconda/bin/python

rootdir: /home/developer/projects/learn/scripts/misc, inifile:

plugins: httpbin, cov

collected 1 items

mock_attributes.py::test_method_under_test True

PASSED

======= 1 passed in 0.03 seconds =================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值