django-rest-frameworklocust

Use the @task decorator or set the 'tasks' attribute of the User (or mark it as abstract = True if you only intend to subclass it)


My wish is to run the locust file separately but here coming issues. In my locustio folder have multiple files named are, base.py, locust_test_adio.py, and locust_test_project.py.

When I run this command ( locust -f common/locustio/locust_test_adio.py ) the get called locust_test_adio.py is working fine. But at the same time when I run this ( locust -f common/locustio/locust_test_project.py ) for triggering thelocust_test_project.py file thrown an error is,

Exception: No tasks defined on BaseLocustLoadTest. Use the @task decorator or set the 'tasks' attribute of the User (or mark it as abstract = True if you only intend to subclass it)

Can you please tell me where is the problem here? I tried different ways but didn't find the right solution.

base.py:

from locust import HttpUser, between

class BaseLocustLoadTest(HttpUser):
    wait_time = between(1, 2)

    def on_start(self):
        self.login()

    def login(self):
        response = self.client.post(
            "api/v1/token", json={"email": "mossaddak@gmail.com", "password": "1234"}
        )
        self.access_token = response.json()["access"]
        self.headers = {"Authorization": f"Bearer {self.access_token}"}

    def request_with_auth(self, method, path, **kwargs):
        return self.client.request(method, path, headers=self.headers, **kwargs)

locust_test_adio.py:

from locust import task
from base import BaseLocustLoadTest


class AdApiLocustLoadTest(BaseLocustLoadTest):
    # All tasks related to ads organization
    @task
    def list_ads_organization(self):
        self.adio_organization_response = self.request_with_auth(
            "GET", "api/v1/ads/organizations"
        )

locust_test_project.py:

from locust import task
from base import BaseLocustLoadTest


class ProjectApiLocustLoadTest(BaseLocustLoadTest):
    @task
    def get_project_list(self):
        self.request_with_auth("GET", "api/v1/we/projects")

Solution

  • Set the abstract property of BaseLocustLoadTest class as abstract to True:

    class BaseLocustLoadTest(HttpUser):
        abstract = True
        ...
    

    This will prevent Locust from attempting to instantiate and run it.