REST / JSON API

关如何调用REST API并验证响应的示例:

from json import JSONDecodeError
...
with self.client.post("/", json={"foo": 42, "bar": None}, catch_response=True) as response:
    try:
        if response.json()["greeting"] != "hello":
            response.failure("Did not get expected value in greeting")
    except JSONDecodeError:
        response.failure("Response could not be decoded as JSON")
    except KeyError:
        response.failure("Response did not contain expected key 'greeting'")

分组请求

网站拥有其网址包含某种动态参数的页面是很常见的。通常,在用户的统计信息中将这些URL分组在一起是很有意义的。这可以通过将name参数传递给HttpSessions 不同的请求方法来完成。

例子:

# Statistics for these requests will be grouped under: /blog/?id=[id]
for i in range(10):
    self.client.get("/blog?id=%i" % i, name="/blog?id=[id]")

HTTP代理设置

为了提高性能,我们通过将request.Session的trust_env属性设置为False。如果不希望这样做,则可以手动设置 locust_instance.client.trust_envTrue

posted @ 2022-04-05 23:01  xaom  阅读(73)  评论(0)    收藏  举报