0

Trying to run my Locust file and while doing so I'm facing the following error while trying command locust -f locustfile.py --host=http://localhost:8080

File "/home/sonali/.local/lib/python3.6/site-packages/locust/user/task.py", line 280, in run self.schedule_task(self.get_next_task()) File "/home/sonali/.local/lib/python3.6/site-packages/locust/user/task.py", line 408, in get_next_task return random.choice(self.user.tasks) File "/usr/lib/python3.6/random.py", line 260, in choice raise IndexError('Cannot choose from an empty sequence') from None Cannot choose from an empty sequence

My locust file is as follows:

from locust import HttpUser, task, between ,TaskSet

class UserBehavior(TaskSet):
    def on_start(self):
        """ on_start is called when a Locust start before 
            any task is scheduled
        """
        self.login()
    def login(self):
        self.client.post("/login",
                         {"username":"ellen_key",
                          "password":"education"})
    @task(2)
    def index(self):
        self.client.get("/")
    @task(1)
    def profile(self):
        self.client.get("/profile")
class WebsiteUser(HttpUser):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000
Sayen
  • 65
  • 1
  • 11

2 Answers2

1

task_set has been renamed to tasks in Locust 1.0

In your case, I would suggest moving everything from your TaskSet directly into the user directly on WebSiteUser (also a new feature of 1.0). Then you dont need to set the tasks/task_set property at all.

See https://docs.locust.io/en/stable/writing-a-locustfile.html#id1 for more info on the tasks attribute, if you still want to use it.

Cyberwiz
  • 11,027
  • 3
  • 20
  • 40
0

You define no tasks in WebsiteUser class. The documentation says:

The behaviour of this user is defined by its tasks. Tasks can be declared either directly on the class by using the @task decorator on methods, or by setting the tasks attribute.

You have neither.

So going further in the documentation, it says about tasks:

Collection of python callables and/or TaskSet classes that the Locust user(s) will run.

Following that advice, you code should look like:

from locust import HttpUser, task, between ,TaskSet

class UserBehavior(TaskSet):
    def on_start(self):
        """ on_start is called when a Locust start before 
            any task is scheduled
        """
        self.login()

    def login(self):
        self.client.post("/login",
                         {"username":"ellen_key",
                          "password":"education"})

    @task(2)
    def index(self):
        self.client.get("/")

    @task(1)
    def profile(self):
        self.client.get("/profile")

class WebsiteUser(HttpUser):
    tasks = [UserBehavior]
    min_wait = 5000
    max_wait = 9000

For more information, read the documentation.

pavelsaman
  • 7,399
  • 1
  • 14
  • 32
  • what if it is basic auth, instead of the POST request on /login? where is documentation on authenticating using basic auth? thanks – uberrebu Sep 29 '20 at 09:01