How to run locust tests with multiple locustfiles. The locust documentation is not very clear on how to do this.
3 Answers
Write tests in different files. Ensure the classes in each of these files are named differently.
import the classes from all different files into a locustfile.py ( it can have any name and need not be locustfile.py )
example.
testfile1.py
from locust import HttpUser, task, between, tag
class WebTests1(HttpUser):
wait_time = between(0,0.1)
def on_start(self):
# on_start is called when a Locust start before any task is scheduled.
pass
def on_stop(self):
# on_stop is called when the TaskSet is stopping
pass
@task(1)
def testaURL1(self):
response = self.client.post("/api/test/url1",
name="test url",
data="some json data",
headers="headers")
testfile2.py
from locust import HttpUser, task, between, tag
class WebTests2(HttpUser):
wait_time = between(0,0.1)
def on_start(self):
# on_start is called when a Locust start before any task is scheduled.
pass
def on_stop(self):
# on_stop is called when the TaskSet is stopping
pass
@task(1)
def testaURL2(2self):
response = self.client.post("/api/test/url2",
name="test url",
data="some json data",
headers="headers")
locustfile.py
from testfile1 import WebTests1
from testfile2 import WebTests2
Save all the files in the same directory and run cmd locust
or locust -f locustfile.py
Its recommend that you follow Python best practices for structuring your test code. Ref : https://docs.locust.io/en/stable/writing-a-locustfile.html#how-to-structure-your-test-code

- 647
- 9
- 15
-
How to pass the configs to WebTests1 and WebTests2? – Anoop K.S Apr 12 '23 at 14:03
-
I have 50 different classes and I want to test 10 concurrent users. But only first 10 classes are selected for test. Is there any way to test 10 user for all 50 classes? – xanjay Jun 13 '23 at 15:24
As per the documentation you can keep multiple locust files in a directory and pass the directory name to the argument -f/--locustfile Locust will recursively search for files with .py extension and run all of them. It ignores any filename starts with "_" and locust.py

- 151
- 1
- 4
According locust documentation in this link, in example you have 3 file named as locustfile1.py, locustfile2.py and locustfile3.py
in directory named /home/user/Files
.
You can run this three files with below script:
locust -f /home/user/Files/locustfile1.py, /home/user/Files/locustfile2.py, /home/user/Files/locustfile3.py
or use this:
locust -f /home/user/Files

- 13
- 5