diff --git a/httprunner/api.py b/httprunner/api.py index 2867bc74..3573a0b7 100644 --- a/httprunner/api.py +++ b/httprunner/api.py @@ -199,3 +199,44 @@ class HttpRunner(object): html_report_name, html_report_template ) + + +def prepare_locust_tests(path): + """ prepare locust testcases + + Args: + path (str): testcase file path. + + Returns: + dict: locust tests data + + { + "functions": {}, + "tests": [] + } + + """ + tests_mapping = loader.load_tests(path) + parser.parse_tests(tests_mapping) + + functions = tests_mapping.get("project_mapping", {}).get("functions", {}) + testcase = tests_mapping["testcases"][0] + items = testcase.get("teststeps", []) + + tests = [] + for item in items: + if "config" in item: + # embeded testcase + weight = item["config"].get("weight", 1) + else: + # API test + weight = item.get("weight", 1) + + # implement weight for tests + for _ in range(weight): + tests.append(item) + + return { + "functions": functions, + "tests": tests + } diff --git a/httprunner/loader.py b/httprunner/loader.py index 0f08f279..16593674 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -661,71 +661,3 @@ def load_tests(path, dot_env_path=None): tests_mapping["testcases"] = testcases_list return tests_mapping - - -def load_locust_tests(path, dot_env_path=None): - """ load locust testcases - - Args: - path (str): testcase/testsuite file path. - dot_env_path (str): specified .env file path - - Returns: - dict: locust testcases with weight - { - "config": {...}, - "tests": [ - # weight 3 - [teststep11], - [teststep11], - [teststep11], - # weight 2 - [teststep21, teststep22], - [teststep21, teststep22] - ] - } - - """ - raw_testcase = load_file(path) - load_project_tests(path, dot_env_path) - - config = {} - tests = [] - for item in raw_testcase: - key, test_block = item.popitem() - - if key == "config": - config.update(test_block) - elif key == "test": - teststep = load_teststep(test_block) - weight = test_block.get("weight", 1) - for _ in range(weight): - tests.append(teststep) - - # parse config variables - raw_config_variables = config.get("variables", []) - - config_variables = parser.parse_data( - raw_config_variables, - {}, - project_mapping["functions"] - ) - - # parse config name - config["name"] = parser.parse_data( - config.get("name", ""), - config_variables, - project_mapping["functions"] - ) - - # parse config request - config["request"] = parser.parse_data( - config.get("request", {}), - config_variables, - project_mapping["functions"] - ) - - return { - "config": config, - "tests": tests - } diff --git a/httprunner/parser.py b/httprunner/parser.py index e764b048..59508565 100644 --- a/httprunner/parser.py +++ b/httprunner/parser.py @@ -860,11 +860,6 @@ def parse_tests(tests_mapping): ] } - variables_mapping (dict): if variables_mapping is specified, it will override variables in config block. - - Returns: - list: parsed testcases list, with config variables/parameters/name/request parsed. - """ project_mapping = tests_mapping.get("project_mapping", {}) diff --git a/httprunner/templates/locustfile_template b/httprunner/templates/locustfile_template index 907086db..32bf497c 100644 --- a/httprunner/templates/locustfile_template +++ b/httprunner/templates/locustfile_template @@ -3,7 +3,7 @@ import random import zmq from httprunner.exceptions import MyBaseError, MyBaseFailure -from httprunner.loader import load_locust_tests +from httprunner.api import prepare_locust_tests from httprunner.runner import Runner from locust import HttpLocust, TaskSet, task from locust.events import request_failure @@ -15,21 +15,20 @@ logging.getLogger('locust.runners').setLevel(logging.INFO) class WebPageTasks(TaskSet): def on_start(self): - self.test_runner = Runner(self.locust.config, self.client) + self.test_runner = Runner(self.locust.config, self.locust.functions, self.client) - @task(weight=1) + @task def test_any(self): - teststeps = random.choice(self.locust.tests) - for teststep in teststeps: - try: - self.test_runner.run_test(teststep) - except (AssertionError, MyBaseError, MyBaseFailure) as ex: - request_failure.fire( - request_type=teststep.get("request", {}).get("method"), - name=teststep.get("request", {}).get("url"), - response_time=0, - exception=ex - ) + test_dict = random.choice(self.locust.tests) + try: + self.test_runner.run_test(test_dict) + except (AssertionError, MyBaseError, MyBaseFailure) as ex: + request_failure.fire( + request_type=test_dict.get("request", {}).get("method"), + name=test_dict.get("request", {}).get("url"), + response_time=0, + exception=ex + ) class WebPageUser(HttpLocust): @@ -38,8 +37,9 @@ class WebPageUser(HttpLocust): max_wait = 30 file_path = "$TESTCASE_FILE" - locust_tests = load_locust_tests(file_path) - config = locust_tests["config"] + locust_tests = prepare_locust_tests(file_path) + functions = locust_tests["functions"] tests = locust_tests["tests"] + config = {} - host = config.get('request', {}).get('base_url', '') + host = config.get('base_url', '') diff --git a/tests/data/demo_locust.yml b/tests/locust_tests/demo_simple_locust.yml similarity index 92% rename from tests/data/demo_locust.yml rename to tests/locust_tests/demo_simple_locust.yml index 8965689c..baab4ae5 100644 --- a/tests/data/demo_locust.yml +++ b/tests/locust_tests/demo_simple_locust.yml @@ -1,7 +1,6 @@ - config: name: basic test with httpbin - request: - base_url: https://httpbin.org/ + base_url: https://httpbin.org/ - test: name: index diff --git a/tests/test_api.py b/tests/test_api.py index 3a1cea67..b6f478da 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,6 +1,7 @@ import os import shutil import time +import unittest from httprunner import HttpRunner, api, loader, parser from locust import HttpLocust @@ -564,4 +565,16 @@ class TestApi(ApiServerUnittest): self.assertEqual( results.records[1]["name"], "create user 1001 and check result." - ) \ No newline at end of file + ) + + +class TestLocust(unittest.TestCase): + + def test_prepare_locust_tests(self): + path = os.path.join( + os.getcwd(), 'tests/locust_tests/demo_simple_locust.yml') + locust_tests = api.prepare_locust_tests(path) + self.assertIn("gen_md5", locust_tests["functions"]) + self.assertEqual(len(locust_tests["tests"]), 10) + self.assertEqual(locust_tests["tests"][0]["name"], "index") + self.assertEqual(locust_tests["tests"][9]["name"], "user-agent") diff --git a/tests/test_loader.py b/tests/test_loader.py index 575802f3..7459e39f 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -444,11 +444,3 @@ class TestSuiteLoader(unittest.TestCase): loader.load_project_tests(os.path.join(os.getcwd(), "tests")) self.assertIn("get_token", self.tests_def_mapping["api"]) self.assertEqual(self.project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH") - - def test_load_locust_tests(self): - path = os.path.join( - os.getcwd(), 'tests/data/demo_locust.yml') - locust_tests = loader.load_locust_tests(path) - self.assertEqual(len(locust_tests["tests"]), 10) - self.assertEqual(locust_tests["tests"][0]["name"], "index") - self.assertEqual(locust_tests["tests"][9]["name"], "user-agent")