我正在为 api 做一个包装器。我希望该函数在输入无效时返回自定义异常消息。
def scrape(date, x, y):
response = requests.post(api_url, json={'date': date, 'x': x, 'y': y})
if response.status_code == 200:
output = loads(response.content.decode('utf-8'))
return output
else:
raise Exception('Invalid input')
这是对它的测试:
from scrape import scrape
def test_scrape():
with pytest.raises(Exception) as e:
assert scrape(date='test', x=0, y=0)
assert str(e.value) == 'Invalid input'
但是覆盖测试由于某种原因跳过了最后一行。有谁知道为什么?我尝试将代码更改为with pytest.raises(Exception, match = 'Invalid input') as e,但出现错误:
AssertionError: Pattern 'Invalid input' not found in "date data 'test' does not match format '%Y-%m-%d %H:%M:%S'"
这是否意味着它实际上是在引用来自 api 而不是我的包装器的异常消息?
莫回无
DIEA
相关分类