我正在测试一个特定的响应代码,并希望在代码不同时模拟出一个测试用例,比如未经授权的401
。我使用的是python3.7http.客户端库和pytest
到目前为止,我尝试使用@patch
修饰符,并用side_effect
调用函数来触发异常
我的测试用例:
from unittest import mock
from application import shorten_url
def mock_status(url):
raise ConnectionError
@patch("application.shorten_url", side_effect=mock_status)
def test_bitly(client):
with pytest.raises(ConnectionError) as e:
shorten_url("something")
我的代码:
^{pr2}$我真的不明白如何使用mock
和side_effect
正确地引发此异常。在
一个朋友帮我解决了这个问题,这似乎很管用(我还是很困惑):
相关问题 更多 >
编程相关推荐