mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-21 12:33:59 +08:00
update locusts with HttpRunner 2.0
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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", {})
|
||||
|
||||
|
||||
@@ -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', '')
|
||||
|
||||
Reference in New Issue
Block a user