Using catch_response in Locust for Custom Response Validation
Example Code: Validating a Response Using catch_response
catch_responsefrom locust import HttpUser, constant, task
class MyReqRes(HttpUser):
wait_time = constant(1)
host = "http://localhost:8001"
@task
def get_todos(self):
# Sending a GET request and catching the response for custom validation
with self.client.get("/todos/104", name="test todo", catch_response=True) as resp1:
# Checking if the response JSON contains the expected owner name
if resp1.status_code == 200 and "Christian Adams" in resp1.json().get("name", ""):
resp1.success() # Mark the response as successful
else:
resp1.failure("Expected owner 'Christian Adams' not found.") # Mark it as a failure
Breakdown of the Code
Why Use catch_response?
catch_response?Sample Output
When to Cache Responses?
Enhancements and Next Steps
PreviousLoad Testing with HTTP POST Requests in LocustNextIterative Load Testing with Step Size in Locust
Last updated