【发布时间】:2014-12-10 01:22:37
【问题描述】:
我有一个实际连接到服务并执行身份验证和其他操作的类,但是所有这些都在我的代码中的其他地方进行了很好的测试,我只想在以下测试中进行模拟:
带有繁琐的 __init__ 的对象
class B(object):
def __init__(self, username, password):
con = connect_to_service() #
auth = con.authenticate(username, password)
def upload(self)
return "Uploaded"
class A(object):
def send():
b = B('user', 'pass')
b.upload()
tests.py
# Now, I want here to test A, and since it uses B, I need to mock it, but I can't get it to work.
def test_A():
# Here I need to mock B and B.upload, I tried this:
a = A()
b = Mock()
b.upload.return_value='Hi'
a.send()
但是这个测试失败了,因为它到达了 B.init 上的 auth(),我想成为一个 Mock 模型。
【问题讨论】:
标签: python django mocking pytest