From 68203e20c36d886d53dfb902f069b8ee0952e216 Mon Sep 17 00:00:00 2001 From: Andrew Baldwin Date: Wed, 11 Oct 2023 17:02:33 +0200 Subject: [PATCH] Add test_html_stats_report --- locust/html.py | 2 +- locust/stats.py | 2 +- locust/test/test_web.py | 27 ++++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/locust/html.py b/locust/html.py index 92490dfc3c..6990c3bba3 100644 --- a/locust/html.py +++ b/locust/html.py @@ -104,7 +104,7 @@ def get_html_report( "response_time_statistics": [ { "name": escape(stat.name), - "method": escape(stat.method), + "method": escape(stat.method or ""), **{ str(percentile): stat.get_response_time_percentile(percentile) for percentile in PERCENTILES_FOR_HTML_REPORT diff --git a/locust/stats.py b/locust/stats.py index ce26534d44..0f1bf9abba 100644 --- a/locust/stats.py +++ b/locust/stats.py @@ -689,7 +689,7 @@ def _cache_response_times(self, t: int) -> None: def to_dict(self, escape_string_values=False): return { - "method": escape(self.method) if escape_string_values else self.method, + "method": escape(self.method or "") if escape_string_values else self.method, "name": escape(self.name) if escape_string_values else self.name, "safe_name": escape(self.name, quote=False), "num_requests": self.num_requests, diff --git a/locust/test/test_web.py b/locust/test/test_web.py index 3ce5ab1e9c..545e1c27d4 100644 --- a/locust/test/test_web.py +++ b/locust/test/test_web.py @@ -993,6 +993,18 @@ def tick(self): self.assertRegex(response.text, re_spawn_rate) self.assertNotRegex(response.text, re_disabled_spawn_rate) + def test_html_stats_report(self): + self.environment.locustfile = "locust.py" + self.environment.host = "http://localhost" + + response = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port) + self.assertEqual(200, response.status_code) + + d = pq(response.content.decode("utf-8")) + + self.assertIn("Script: locust.py", str(d)) + self.assertIn("Target Host: http://localhost", str(d)) + class TestWebUIAuth(LocustTestCase): def setUp(self): @@ -1133,7 +1145,7 @@ def test_request_stats_full_history_csv(self): self.assertEqual("Aggregated", rows[3][3]) -class TestModernWebUi(LocustTestCase, _HeaderCheckMixin): +class TestModernWebUI(LocustTestCase, _HeaderCheckMixin): def setUp(self): super().setUp() @@ -1186,3 +1198,16 @@ def test_web_ui_no_runner(self): self.assertEqual("Error: Locust Environment does not have any runner", response.text) finally: web_ui.stop() + + def test_html_stats_report(self): + self.environment.locustfile = "locust.py" + self.environment.host = "http://localhost" + + response = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port) + self.assertEqual(200, response.status_code) + + d = pq(response.content.decode("utf-8")) + + self.assertTrue(d("#root")) + self.assertIn('"locustfile": "locust.py"', str(d)) + self.assertIn('"host": "http://localhost"', str(d))