简体   繁体   中英

How to assert a URL was not requested with HTTPretty

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM