How to run locust with multiple test files

Viewed 3536

How to run locust tests with multiple locustfiles. The locust documentation is not very clear on how to do this.

1 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

Related