Ahs*_*que 2 python python-mock python-unittest python-3.5
我想测试的这个演示功能非常简单。
def is_email_deliverable(email):
try:
return external.verify(email)
except Exception:
logger.error("External failed failed")
return False
Run Code Online (Sandbox Code Playgroud)
此功能使用external我想模拟的服务。
但我无法弄清楚如何exception从external.verify(email) 即如何强制except执行该子句。
我的尝试:
@patch.object(other_module, 'external')
def test_is_email_deliverable(patched_external):
def my_side_effect(email):
raise Exception("Test")
patched_external.verify.side_effects = my_side_effect
# Or,
# patched_external.verify.side_effects = Exception("Test")
# Or,
# patched_external.verify.side_effects = Mock(side_effect=Exception("Test"))
assert is_email_deliverable("some_mail@domain.com") == False
Run Code Online (Sandbox Code Playgroud)
这个问题声称有答案,但对我不起作用。
您已使用side_effects代替side_effect. 它是这样的
@patch.object(Class, "attribute")
def foo(attribute):
attribute.side_effect = Exception()
# Other things can go here
Run Code Online (Sandbox Code Playgroud)
顺便说一句,Exception根据它捕获所有并处理它并不是一个好方法。
| 归档时间: |
|
| 查看次数: |
9227 次 |
| 最近记录: |