From a70b3b5e649c89be77321332bd9d7af3233ce362 Mon Sep 17 00:00:00 2001 From: Anders Aaen Springborg Date: Tue, 27 Dec 2022 14:46:33 -0500 Subject: [PATCH] json schema integration test --- locust/test/test_main.py | 53 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/locust/test/test_main.py b/locust/test/test_main.py index 663fe3f7e8..1ef19bfe6d 100644 --- a/locust/test/test_main.py +++ b/locust/test/test_main.py @@ -1414,16 +1414,49 @@ def t(self): pass """ ) + with mock_locustfile(content=LOCUSTFILE_CONTENT) as mocked: + proc = subprocess.Popen( + ["locust", "-f", mocked.file_path, "--headless", "-t", "5s", "--json"], + stderr=DEVNULL, + stdout=PIPE, + text=True, + ) + stdout, stderr = proc.communicate() + + try: + json.loads(stdout) + except json.JSONDecodeError: + self.fail(f"Trying to parse {stdout} as json failed") + self.assertEqual(0, proc.returncode) + + def test_json_schema(self): + LOCUSTFILE_CONTENT = textwrap.dedent( + """ + from locust import HttpUser, task, constant + + class QuickstartUser(HttpUser): + wait_time = constant(1) + + @task + def hello_world(self): + self.client.get("/") + + """ + ) with mock_locustfile(content=LOCUSTFILE_CONTENT) as mocked: proc = subprocess.Popen( [ "locust", "-f", mocked.file_path, + "--host", + "http://google.com", "--headless", + "-u", + "1", "-t", - "5s", - "--json" + "2s", + "--json", ], stderr=DEVNULL, stdout=PIPE, @@ -1432,11 +1465,25 @@ def t(self): stdout, stderr = proc.communicate() try: - json.loads(stdout) + data = json.loads(stdout) except json.JSONDecodeError: self.fail(f"Trying to parse {stdout} as json failed") + self.assertEqual(0, proc.returncode) + result = data[0] + self.assertEqual(float, type(result["last_request_timestamp"])) + self.assertEqual(float, type(result["start_time"])) + self.assertEqual(int, type(result["num_requests"])) + self.assertEqual(int, type(result["num_none_requests"])) + self.assertEqual(float, type(result["total_response_time"])) + self.assertEqual(float, type(result["max_response_time"])) + self.assertEqual(float, type(result["min_response_time"])) + self.assertEqual(int, type(result["total_content_length"])) + self.assertEqual(dict, type(result["response_times"])) + self.assertEqual(dict, type(result["num_reqs_per_sec"])) + self.assertEqual(dict, type(result["num_fail_per_sec"])) + def test_worker_indexes(self): content = """ from locust import HttpUser, task, between