diff --git a/locust/core.py b/locust/core.py index 63cfe7807e..c228af1970 100644 --- a/locust/core.py +++ b/locust/core.py @@ -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): diff --git a/locust/test/test_locust_class.py b/locust/test/test_locust_class.py index 0a3d378b4f..df621d741a 100644 --- a/locust/test/test_locust_class.py +++ b/locust/test/test_locust_class.py @@ -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 @@ -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