Skip to content

Commit

Permalink
Merge pull request #1287 from locustio/generate-more-descriptive-erro…
Browse files Browse the repository at this point in the history
…r-when-user-forgot-do-define-tasks

Give a more descriptive error when the Locust or TaskSet has no tasks.
  • Loading branch information
cyberw authored Mar 16, 2020
2 parents 3d8b3c1 + b330585 commit d8be05a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions locust/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ def schedule_task(self, task_callable, args=None, kwargs=None, first=False):
self._task_queue.append(task)

def get_next_task(self):
if not self.tasks:
raise Exception("No tasks defined. use the @task decorator or set the tasks property of the TaskSet")
return random.choice(self.tasks)

def wait_time(self):
Expand Down
15 changes: 15 additions & 0 deletions locust/test/test_locust_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ class MyTasks(TaskSet):
self.assertEqual(t1_count, 5)
self.assertEqual(t2_count, 2)

def test_tasks_missing_gives_user_friendly_exception(self):
class User(Locust):
wait_time = constant(0)
tasks = None
_catch_exceptions = False

class MyTasks(TaskSet):
tasks = None

l = MyTasks(User(self.environment))
self.assertRaisesRegex(Exception, "No tasks defined.*", l.run)
l.tasks = None
self.assertRaisesRegex(Exception, "No tasks defined.*", l.run)

def test_task_decorator_ratio(self):
t1 = lambda l: None
t2 = lambda l: None
Expand Down Expand Up @@ -178,6 +192,7 @@ def t1(self):
taskset = MyTaskSet3(self.locust)
self.assertEqual(len(taskset.tasks), 3)


def test_wait_function(self):
class MyTaskSet(TaskSet):
a = 1
Expand Down

0 comments on commit d8be05a

Please sign in to comment.