diff --git a/httprunner/api.py b/httprunner/api.py index 98b383be..52f4345d 100644 --- a/httprunner/api.py +++ b/httprunner/api.py @@ -244,21 +244,27 @@ def prepare_locust_tests(path): parsed_tests_mapping = parser.parse_tests(tests_mapping) functions = parsed_tests_mapping.get("project_mapping", {}).get("functions", {}) - testcase = parsed_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) + for testcase in parsed_tests_mapping["testcases"]: + testcase_weight = testcase.get("config", {}).get("weight", 1) + items = testcase.get("teststeps", []) + + testcase_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): + testcase_tests.append(item) + + tests.extend(testcase_tests * testcase_weight) return { "functions": functions, diff --git a/httprunner/parser.py b/httprunner/parser.py index fc1cad58..cb63600c 100644 --- a/httprunner/parser.py +++ b/httprunner/parser.py @@ -942,6 +942,9 @@ def __get_parsed_testsuite_testcases(testcases, testsuite_config, project_mappin parsed_testcase["path"] = testcase["testcase"] parsed_testcase["config"]["name"] = testcase_name + if "weight" in testcase: + parsed_testcase["config"]["weight"] = testcase["weight"] + # base_url priority: testcase config > testsuite config parsed_testcase["config"].setdefault("base_url", testsuite_base_url) diff --git a/tests/locust_tests/demo_locust_with_layer.yml b/tests/locust_tests/demo_locust_with_layer.yml new file mode 100644 index 00000000..352d4932 --- /dev/null +++ b/tests/locust_tests/demo_locust_with_layer.yml @@ -0,0 +1,18 @@ +config: + name: create users with uid + variables: + - device_sn: ${gen_random_string(15)} + base_url: "http://127.0.0.1:5000" + +testcases: + create user 1000 and check result.: + testcase: testcases/create_and_check.yml + weight: 2 + variables: + uid: 1000 + + create user 1001 and check result.: + testcase: testcases/create_and_check.yml + weight: 3 + variables: + uid: 1001 diff --git a/tests/test_api.py b/tests/test_api.py index 2a976ca4..e0da1d1a 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -598,3 +598,12 @@ class TestLocust(unittest.TestCase): self.assertEqual(len(locust_tests["tests"]), 10) self.assertEqual(locust_tests["tests"][0]["name"], "index") self.assertEqual(locust_tests["tests"][9]["name"], "user-agent") + + def test_prepare_locust_tests_with_layer(self): + path = os.path.join( + os.getcwd(), 'tests/locust_tests/demo_locust_with_layer.yml') + locust_tests = prepare_locust_tests(path) + self.assertIn("gen_md5", locust_tests["functions"]) + self.assertEqual(len(locust_tests["tests"]), 4 * 2 + 4 * 3) + self.assertIn("setup and reset all (override)", locust_tests["tests"][0]["config"]["name"]) + self.assertIn("check if user ", locust_tests["tests"][19]["name"])