Enhancing Your Locust Tests with Custom Logging
Why Use Logging in Locust?
Setting Up Logging in Locust
Example: Logging in a Locust Test
from locust import HttpUser, constant, task
import logging
# Initialize a logger
logger = logging.getLogger("locust")
logger.setLevel(logging.INFO) # Set log level to INFO
class MyReqRes(HttpUser):
wait_time = constant(1) # Wait 1 second between tasks
host = "http://localhost:8001" # Set the target host
@task
def get_todos(self):
# Send a GET request to the "/todos/104" endpoint
with self.client.get("/todos/104", name="test todo", catch_response=True) as resp1:
if resp1.status_code == 200 and "Christian Adams" in resp1.json().get("name", ""):
# Log a success message if the condition is met
logger.info("Successfully found user 'Christian Adams'.")
resp1.success()
else:
# Log a warning message if the condition is not met
logger.warning(f"Failed to validate user. Response: {resp1.text}")
resp1.failure("User validation failed.")
Breakdown of the Code
Running the Test
PreviousIterative Load Testing with Step Size in LocustNextRunning Locust in Distributed Mode with Master and Workers
Last updated