Home > OS >  How to make Locust test my API endpoint served with FastAPI?
How to make Locust test my API endpoint served with FastAPI?

Time:07-24

I have an API served with FastAPI working on:

http://127.0.0.1:8000/predictions

And I want to test it using Locust. My code:

from locust import HttpUser, TaskSet, task
import json
 
class UserBehavior(TaskSet):
 
    @task(1)    
    def create_post(self):
        headers = {'content-type': 'application/json','Accept-Encoding':'gzip'}
        self.client.post("/predictions",data= json.dumps({
      "text": "I am tired",
    }), 
    headers=headers, 
    name = "Create a new post")
 
 
class WebsiteUser(HttpUser):
    task=[UserBehavior]

I get this error msg while locust is running:

    2022-07-23 16:33:32,764] pop-os/ERROR/locust.user.task: No tasks defined on WebsiteUser. 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)
Traceback (most recent call last):
  File "/home/statspy/anaconda3/lib/python3.9/site-packages/locust/user/task.py", line 340, in run
    self.schedule_task(self.get_next_task())
  File "/home/statspy/anaconda3/lib/python3.9/site-packages/locust/user/task.py", line 472, in get_next_task
    raise Exception(
Exception: No tasks defined on WebsiteUser. 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)

How can I fix it?

Thanks

CodePudding user response:

Its tasks=[UserBehavior] , not task=[UserBehavior]

  • Related