1

在 Python 单元测试中使用 HTTPretty 拦截 HTTP 请求时,如何断言从未请求过目标 URL?

4

1 回答 1

0

By default, if a request doesn't match any registered URI it goes through.

One solution would be to add a generic matcher using Matching regular expressions and making it failing.

>>> httpretty.register_uri(httpretty.GET,
...                        re.compile(".*"),
...                        status=500,
...                        body="Fail")
...
>>> requests.get("http://www.google.ch/")
<Response [200]>
>>> httpretty.activate()
>>> requests.get("http://www.google.ch/")
<Response [500]>

Of course, your application must fail in such cases. Another idea might be to use record.

Another idea would be to use HTTPretty.record and then analyze the produced file. But it feels a bit more complicated.

于 2014-06-01T16:34:20.253 回答