Registry / testing / locust

locust

JSON →
library2.46.4pypypi✓ verified 25d ago

Locust is an open-source, developer-friendly load testing framework that allows you to define user behavior in plain Python code. It's designed for testing web applications, APIs, and other systems, supporting hundreds of thousands of concurrent users through its event-based architecture. Locust offers a real-time web-based UI for monitoring and analysis, and is actively maintained with frequent updates (approximately every 62 days) [1, 2, 11, 19, 31]. It currently requires Python 3.10 or newer [31].

pip install locust
INSTALL
IMPORT
SIG · LOCUST
L
locust
testingpythonv2.46.4
Install
7.0s avg
Import
1249ms
Disk
75MB
Pass rate
8/ 10
Env Coverage8 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.46.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✓ —
✓ 8s
py 3.11
✓ —
✓ 7.4s
py 3.12
✓ —
✓ 6.3s
py 3.13
✓ —
✓ 6.3s
py 3.9
✕ build_error
✕ build_error
75MB installed
● package 75MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HttpUser
from locust import HttpUser
from locust import HttpLocust
`HttpLocust` was renamed to `HttpUser` in version 2.0.0 [25, 29].
task
from locust import task
Used as a decorator for methods within `User` or `HttpUser` classes to define user actions [20].
between
from locust import between
A built-in wait_time function for simulating realistic user think times [20].
User
from locust import User
from locust import Locust
The base `Locust` class was renamed to `User` in version 2.0.0 [25, 29]. `HttpUser` is typically used for web testing [20, 21].

This quickstart defines a `QuickstartUser` that inherits from `HttpUser` for HTTP load testing. It includes `wait_time` to simulate user think time and two tasks (`hello_world` and `view_items`) decorated with `@task` to define user actions. The `view_items` task demonstrates using the `name` parameter for grouping dynamic URLs. `on_start` and `on_stop` methods are shown for per-user setup and teardown, such as login/logout. Save this code as `locustfile.py` and run `locust` from the command line to start the web UI, or use the `--headless` option for command-line execution [2, 10, 20].

import os from locust import HttpUser, task, between class QuickstartUser(HttpUser): wait_time = between(1, 2) # Users wait between 1 and 2 seconds after each task host = os.environ.get('LOCUST_TARGET_HOST', 'http://localhost:8089') # Default host for testing def on_start(self): """ on_start is called when a Locust user starts running """ # Example: Simulating a login if required by the target system # self.client.post("/login", json={"username":"test_user", "password":"test_password"}) print(f"Starting user on host: {self.host}") @task def hello_world(self): self.client.get("/hello") self.client.get("/world") @task(3) def view_items(self): # Simulate viewing items with dynamic IDs, using 'name' to aggregate stats for item_id in range(10): self.client.get(f"/item?id={item_id}", name="/item") def on_stop(self): """ on_stop is called when a Locust user stops running """ # Example: Simulating a logout # self.client.post("/logout") print("Stopping user.") # To run this, save as `locustfile.py` and execute `locust` from the terminal. # Then open your browser to http://localhost:8089 to use the web UI. # Alternatively, run headless: `locust -f locustfile.py --headless --users 10 --spawn-rate 5 -H http://your-target-host.com`
locust --version
Debug
Known issues
breakingThe primary user classes `Locust` and `HttpLocust` were renamed to `User` and `HttpUser` respectively in version 2.0.0. Older scripts using `HttpLocust` will fail.
fix
Update imports from `from locust import HttpLocust` to `from locust import HttpUser`. Similarly, `Locust` becomes `User` [25, 29].
affects: >=2.0.0
deprecatedThe `min_wait`, `max_wait`, and `wait_function` attributes on `User` classes are deprecated. Use the `wait_time` attribute instead, which should be set to a function (like `between`, `constant`, `constant_throughput`) or a custom callable [27].
fix
Replace `min_wait = X` and `max_wait = Y` with `wait_time = between(X, Y)`. For custom logic, define a `wait_time` method on your `User` class [20, 27].
affects: >=2.0.0
gotchaWhen testing URLs with dynamic components (e.g., `/item?id=1`, `/item?id=2`), Locust will, by default, record each unique URL separately in statistics. This can lead to an explosion of metrics and make analysis difficult. To aggregate these, use the `name` parameter in your client request.
fix
For requests like `self.client.get(f'/item?id={item_id}')`, add `name='/item'` (e.g., `self.client.get(f'/item?id={item_id}', name='/item')`) to group statistics under a common label [20, 21, 36].
affects: All versions
gotchaThe `TaskSet` class (an advanced feature for grouping tasks) does not automatically return control to its parent `User` or `TaskSet`. If not explicitly interrupted, a user entering a `TaskSet` will remain executing its tasks indefinitely.
fix
Call `self.interrupt()` within a `TaskSet` method to explicitly exit the `TaskSet` and allow the user to pick other tasks from its parent [22, 24, 26].
affects: All versions
gotchaLocust has dependencies like `gevent` and `geventhttpclient` which are compiled from C code. This can lead to installation failures if the necessary build tools (e.g., C compiler, Python development headers) are not present on the system.
fix
Ensure that Python development headers and a C compiler (e.g., build-essential on Linux, Xcode command line tools on macOS, Visual C++ Build Tools on Windows) are installed. Using `pip install --prefer-binary locust` can sometimes help by forcing the use of pre-compiled wheels if available [16, 30].
affects: All versions
gotchaBy default, Locust looks for a test script named `locustfile.py`. If your script has a different name, or is not in the current directory, Locust will not find it.
fix
Ensure your test script is named `locustfile.py` or specify the filename using the `-f` flag when running Locust (e.g., `locust -f my_test_script.py`) [4, 9].
affects: All versions
gotchaOnly checking HTTP status codes (e.g., 200 OK) is often insufficient for validating successful user flows. A server might return 200 OK with an error message in the body, or simply not return the expected content.
fix
Always add assertions to validate the response content, not just the status code. Use Python's built-in assertion capabilities on `response.text` or `response.json()` and call `response.failure("Reason for failure")` if validation fails [34].
affects: All versions
Errors
Common errors & fixes
locust: command not found
The 'locust' executable is not found in your system's PATH environment variable after installation, or the Python environment where Locust was installed is not active.
fix
Ensure your Python environment (e.g., virtual environment) is activated. If installed via pip, the executable might be in a local bin directory (e.g., `~/.local/bin` on Linux/macOS or `Scripts` folder in Python installation on Windows) which needs to be added to your system's PATH. Alternatively, run Locust using `python -m locust -f your_locustfile.py`.
ModuleNotFoundError: No module named 'your_custom_module'
Python cannot find a module imported within your locustfile, typically a custom module or package that is not in the current working directory or Python's `sys.path`.
fix
Run Locust from the root directory of your project, ensuring the imported module is discoverable by Python. For example, if 'your_custom_module.py' is in a 'lib' subdirectory, run `locust -f tests/locustfile.py` from the project root (where 'lib' is a sibling of 'tests'), or add the module's parent directory to `sys.path` within your locustfile: `import sys, os; sys.path.append(os.path.join(os.path.dirname(__file__), '..'))`.
Exception: No tasks defined. use the @task decorator or set the tasks property of the User (or mark it as abstract = True if you only intend to subclass it)
A `User` or `TaskSet` class in your locustfile is defined without any tasks (methods decorated with `@task`) or without explicitly setting its `tasks` attribute. This also occurs if a base class intended for inheritance is instantiated by Locust without being marked as `abstract = True`.
fix
For `User` or `TaskSet` classes meant to be executed, define tasks using the `@task` decorator on methods or assign a list/dictionary of tasks to the `tasks` attribute. For base classes that should not be instantiated directly by Locust, add `abstract = True` to the class definition: `class MyBaseUser(HttpUser): abstract = True`.
AttributeError: 'NoneType' object has no attribute 'environment'
This usually happens when a custom `User` or `TaskSet` class overrides the `__init__` method but fails to call the parent class's `__init__` method (e.g., `super().__init__(parent)` for `TaskSet` or `super().__init__(*args, **kwargs)` for `User`), which is necessary to initialize core Locust attributes like `environment`.
fix
Ensure that any overridden `__init__` methods in your `User` or `TaskSet` classes correctly call their parent's constructor: `class MyTaskSet(TaskSet): def __init__(self, parent): super().__init__(parent) # ... your custom initialization`.
User.host must be set, either via the host attribute or the --host command line argument.
The `HttpUser` class (or its subclasses) in your locustfile does not have a `host` attribute defined, and the `--host` command-line argument was not provided when running Locust.
fix
Define the target host within your `HttpUser` class: `class MyUser(HttpUser): host = 'http://localhost:8080'` or provide it via the command line when starting Locust: `locust -f your_locustfile.py --host http://localhost:8080`.
Upgrade
Version history
2.46.4latest on PyPI · released Aug 24, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.10 or newer for current versions.
geventrequiredCore dependency for asynchronous operations, often requires C build tools for compilation on some systems.
geventhttpclientrequiredAlternative, higher-performance HTTP client for HttpUser, often requires C build tools for compilation on some systems.
Agent activity
5 hits · last 30 days
node
4
Resources