From 1778381e3e23841fa95bb44d9cbba133e2221be1 Mon Sep 17 00:00:00 2001 From: Andrew Baldwin Date: Sun, 5 May 2024 19:49:25 -0400 Subject: [PATCH] Update tests for modern ui --- locust/main.py | 3 -- locust/test/test_fasthttp.py | 10 ++-- locust/test/test_main.py | 13 +---- locust/test/test_web.py | 101 +++++++++-------------------------- 4 files changed, 34 insertions(+), 93 deletions(-) diff --git a/locust/main.py b/locust/main.py index dbaf737bf3..148d2b78aa 100644 --- a/locust/main.py +++ b/locust/main.py @@ -178,9 +178,6 @@ def is_valid_percentile(parameter): sys.stderr.write("[DEPRECATED] The --hatch-rate parameter has been renamed --spawn-rate\n") options.spawn_rate = options.hatch_rate - if options.legacy_ui: - sys.stderr.write("[DEPRECATED] The legacy UI is deprecated and will be removed soon\n") - # setup logging if not options.skip_log_setup: if options.loglevel.upper() in ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]: diff --git a/locust/test/test_fasthttp.py b/locust/test/test_fasthttp.py index f71ae73435..d257fe4e31 100644 --- a/locust/test/test_fasthttp.py +++ b/locust/test/test_fasthttp.py @@ -11,6 +11,7 @@ import gevent from geventhttpclient.client import HTTPClientPool +from pyquery import PyQuery as pq from .testcases import LocustTestCase, WebserverTestCase from .util import create_tls_cert @@ -777,7 +778,8 @@ def tearDown(self): def test_ssl_request_insecure(self): s = FastHttpSession(self.environment, "https://127.0.0.1:%i" % self.web_port, insecure=True, user=None) - r = s.get("/") - self.assertEqual(200, r.status_code) - self.assertIn("Locust for None", r.content.decode("utf-8")) - self.assertIn("

Script: None

", r.text) + response = s.get("/") + d = pq(response.content.decode("utf-8")) + + self.assertEqual(200, response.status_code) + self.assertIn('"users": null', str(d)) diff --git a/locust/test/test_main.py b/locust/test/test_main.py index ecce05f07a..2e6d6e8197 100644 --- a/locust/test/test_main.py +++ b/locust/test/test_main.py @@ -1136,18 +1136,9 @@ def test_html_report_option(self): with open(html_report_file_path, encoding="utf-8") as f: html_report_content = f.read() - # make sure title appears in the report _, locustfile = os.path.split(mocked.file_path) - self.assertIn(f"Test Report for {locustfile}", html_report_content) - self.assertIn(f"

Script: {locustfile}

", html_report_content) - - # make sure host appears in the report - self.assertIn("https://test.com/", html_report_content) - - # make sure the charts container appears in the report - self.assertIn("charts-container", html_report_content) - - self.assertNotIn("Download the Report", html_report_content, "Download report link found in HTML content") + self.assertIn('"host": "https://test.com/"', html_report_content) + self.assertIn('"show_download_link": false', html_report_content) def test_run_with_userclass_picker(self): with temporary_file(content=MOCK_LOCUSTFILE_CONTENT_A) as file1: diff --git a/locust/test/test_web.py b/locust/test/test_web.py index e8c904be23..d317c603b8 100644 --- a/locust/test/test_web.py +++ b/locust/test/test_web.py @@ -105,11 +105,11 @@ def test_index(self): def test_index_with_spawn_options(self): html_to_option = { - "user_count": ["-u", "100"], + "num_users": ["-u", "100"], "spawn_rate": ["-r", "10.0"], } + for html_name_to_test in html_to_option.keys(): - # Test that setting each spawn option individually populates the corresponding field in the html, and none of the others self.environment.parsed_options = parse_options(html_to_option[html_name_to_test]) response = requests.get("http://127.0.0.1:%i/" % self.web_port) @@ -117,15 +117,7 @@ def test_index_with_spawn_options(self): d = pq(response.content.decode("utf-8")) - for html_name in html_to_option.keys(): - start_value = d(f".start [name={html_name}]").attr("value") - edit_value = d(f".edit [name={html_name}]").attr("value") - if html_name_to_test == html_name: - self.assertEqual(html_to_option[html_name][1], start_value) - self.assertEqual(html_to_option[html_name][1], edit_value) - else: - self.assertEqual("1", start_value, msg=f"start value was {start_value} for {html_name}") - self.assertEqual("1", edit_value, msg=f"edit value was {edit_value} for {html_name}") + self.assertIn(f'"{html_name_to_test}": {html_to_option[html_name_to_test][1]}', str(d)) def test_stats_no_data(self): self.assertEqual(200, requests.get("http://127.0.0.1:%i/stats/requests" % self.web_port).status_code) @@ -944,40 +936,42 @@ class MyUser2(User): response = requests.get("http://127.0.0.1:%i/" % self.web_port) self.assertEqual(200, response.status_code) self.assertNotIn("http://example.com", response.content.decode("utf-8")) - self.assertIn("setting this will override the host on all User classes", response.content.decode("utf-8")) def test_report_page(self): self.stats.log_request("GET", "/test", 120, 5612) r = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port) + + d = pq(r.content.decode("utf-8")) + self.assertEqual(200, r.status_code) - self.assertIn("Test Report for None", r.text) - self.assertIn("

Script: None

", r.text) - self.assertIn("charts-container", r.text) - self.assertIn( - 'Download the Report', - r.text, - "Download report link not found in HTML content", - ) + self.assertIn('"host": "None"', str(d)) + self.assertIn('"num_requests": 1', str(d)) + self.assertIn('"is_report": true', str(d)) + self.assertIn('"show_download_link": true', str(d)) def test_report_page_empty_stats(self): r = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port) self.assertEqual(200, r.status_code) - self.assertIn("Test Report for None", r.text) - self.assertIn("charts-container", r.text) def test_report_download(self): self.stats.log_request("GET", "/test", 120, 5612) r = requests.get("http://127.0.0.1:%i/stats/report?download=1" % self.web_port) + + d = pq(r.content.decode("utf-8")) + self.assertEqual(200, r.status_code) self.assertIn("attachment", r.headers.get("Content-Disposition", "")) - self.assertNotIn("Download the Report", r.text, "Download report link found in HTML content") + self.assertIn('"show_download_link": false', str(d)) def test_report_host(self): self.environment.host = "http://test.com" self.stats.log_request("GET", "/test", 120, 5612) r = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port) + + d = pq(r.content.decode("utf-8")) + self.assertEqual(200, r.status_code) - self.assertIn("http://test.com", r.text) + self.assertIn('"host": "http://test.com"', str(d)) def test_report_host2(self): class MyUser(User): @@ -991,8 +985,11 @@ def my_task(self): self.environment.user_classes = [MyUser] self.stats.log_request("GET", "/test", 120, 5612) r = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port) + + d = pq(r.content.decode("utf-8")) + self.assertEqual(200, r.status_code) - self.assertIn("http://test2.com", r.text) + self.assertIn('"host": "http://test2.com"', str(d)) def test_report_exceptions(self): try: @@ -1003,8 +1000,10 @@ def test_report_exceptions(self): self.runner.log_exception("local", str(e), "".join(traceback.format_tb(tb))) self.stats.log_request("GET", "/test", 120, 5612) r = requests.get("http://127.0.0.1:%i/stats/report" % self.web_port) - # self.assertEqual(200, r.status_code) - self.assertIn("

Exceptions Statistics

", r.text) + + d = pq(r.content.decode("utf-8")) + + self.assertIn('exceptions_statistics": [{"count": 2', str(d)) # Prior to 088a98bf8ff4035a0de3becc8cd4e887d618af53, the "nodes" field for each exception in # "self.runner.exceptions" was accidentally mutated in "get_html_report" to a string. @@ -1014,54 +1013,6 @@ def test_report_exceptions(self): isinstance(next(iter(self.runner.exceptions.values()))["nodes"], set), "exception object has been mutated" ) - def test_custom_shape_deactivate_num_users_and_spawn_rate(self): - class TestShape(LoadTestShape): - def tick(self): - return None - - self.environment.shape_class = TestShape - - response = requests.get("http://127.0.0.1:%i/" % self.web_port) - self.assertEqual(200, response.status_code) - - # regex to match the intended select tag with id from the custom argument - re_disabled_user_count = re.compile( - r"]*id=\"(new_)?user_count\"[^>]*disabled=\"disabled\"[^>]*>", flags=re.I - ) - self.assertRegex(response.text, re_disabled_user_count) - - re_disabled_spawn_rate = re.compile( - r"]*id=\"(new_)?spawn_rate\"[^>]*disabled=\"disabled\"[^>]*>", flags=re.I - ) - self.assertRegex(response.text, re_disabled_spawn_rate) - - def test_custom_shape_with_use_common_options_keep_num_users_and_spawn_rate(self): - class TestShape(LoadTestShape): - use_common_options = True - - def tick(self): - return None - - self.environment.shape_class = TestShape - - response = requests.get("http://127.0.0.1:%i/" % self.web_port) - self.assertEqual(200, response.status_code) - - # regex to match the intended select tag with id from the custom argument - re_user_count = re.compile(r"]*id=\"(new_)?user_count\"[^>]*>", flags=re.I) - re_disabled_user_count = re.compile( - r"]*id=\"(new_)?user_count\"[^>]*disabled=\"disabled\"[^>]*>", flags=re.I - ) - self.assertRegex(response.text, re_user_count) - self.assertNotRegex(response.text, re_disabled_user_count) - - re_spawn_rate = re.compile(r"]*id=\"(new_)?spawn_rate\"[^>]*>", flags=re.I) - re_disabled_spawn_rate = re.compile( - r"]*id=\"(new_)?spawn_rate\"[^>]*disabled=\"disabled\"[^>]*>", flags=re.I - ) - 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"