diff --git a/locust/runners.py b/locust/runners.py index 1927dc143b..d3418eda8f 100644 --- a/locust/runners.py +++ b/locust/runners.py @@ -318,7 +318,7 @@ def start(self, user_count: int, spawn_rate: float, wait: bool = False): raise ValueError("wait is True but the amount of users to add is greater than the spawn rate") for user_class in self.user_classes: - if self.environment.host is not None: + if self.environment.host: user_class.host = self.environment.host if self.state != STATE_INIT and self.state != STATE_STOPPED: @@ -674,7 +674,7 @@ def start(self, user_count: int, spawn_rate: float, **kwargs) -> None: return for user_class in self.user_classes: - if self.environment.host is not None: + if self.environment.host: user_class.host = self.environment.host self.spawn_rate = spawn_rate @@ -1085,7 +1085,7 @@ def start_worker(self, user_classes_count: Dict[str, int], **kwargs): self.worker_state = STATE_SPAWNING for user_class in self.user_classes: - if self.environment.host is not None: + if self.environment.host: user_class.host = self.environment.host user_classes_spawn_count = {} diff --git a/locust/test/test_runners.py b/locust/test/test_runners.py index ffa3f5f4fa..a2ff558141 100644 --- a/locust/test/test_runners.py +++ b/locust/test/test_runners.py @@ -474,6 +474,38 @@ def my_task(self): runner.quit() + def test_host_class_attribute_from_web(self): + """If host is left empty from the webUI, we should not use it""" + + class MyUser1(User): + host = "https://host1.com" + + @task + def my_task(self): + pass + + class MyUser2(User): + host = "https://host2.com" + + @task + def my_task(self): + pass + + opts = mocked_options() + # If left empty on the web, we get an empty string as host + opts.host = "" + environment = create_environment([MyUser1, MyUser2], opts) + runner = LocalRunner(environment) + # Start the runner to trigger problematic code + runner.start(user_count=2, spawn_rate=1, wait=False) + runner.spawning_greenlet.join() + + # Make sure we did not overwrite the host variable + self.assertEqual(MyUser1.host, "https://host1.com") + self.assertEqual(MyUser2.host, "https://host2.com") + + runner.quit() + def test_custom_message(self): class MyUser(User): wait_time = constant(1)