update locusts with HttpRunner 2.0

This commit is contained in:
debugtalk
2018-11-23 17:14:43 +08:00
parent 24e6b408e4
commit 8f4500566d
7 changed files with 73 additions and 101 deletions
+41
View File
@@ -199,3 +199,44 @@ class HttpRunner(object):
html_report_name, html_report_name,
html_report_template 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
}
-68
View File
@@ -661,71 +661,3 @@ def load_tests(path, dot_env_path=None):
tests_mapping["testcases"] = testcases_list tests_mapping["testcases"] = testcases_list
return tests_mapping 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
}
-5
View File
@@ -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", {}) project_mapping = tests_mapping.get("project_mapping", {})
+17 -17
View File
@@ -3,7 +3,7 @@ import random
import zmq import zmq
from httprunner.exceptions import MyBaseError, MyBaseFailure 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 httprunner.runner import Runner
from locust import HttpLocust, TaskSet, task from locust import HttpLocust, TaskSet, task
from locust.events import request_failure from locust.events import request_failure
@@ -15,21 +15,20 @@ logging.getLogger('locust.runners').setLevel(logging.INFO)
class WebPageTasks(TaskSet): class WebPageTasks(TaskSet):
def on_start(self): 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): def test_any(self):
teststeps = random.choice(self.locust.tests) test_dict = random.choice(self.locust.tests)
for teststep in teststeps: try:
try: self.test_runner.run_test(test_dict)
self.test_runner.run_test(teststep) except (AssertionError, MyBaseError, MyBaseFailure) as ex:
except (AssertionError, MyBaseError, MyBaseFailure) as ex: request_failure.fire(
request_failure.fire( request_type=test_dict.get("request", {}).get("method"),
request_type=teststep.get("request", {}).get("method"), name=test_dict.get("request", {}).get("url"),
name=teststep.get("request", {}).get("url"), response_time=0,
response_time=0, exception=ex
exception=ex )
)
class WebPageUser(HttpLocust): class WebPageUser(HttpLocust):
@@ -38,8 +37,9 @@ class WebPageUser(HttpLocust):
max_wait = 30 max_wait = 30
file_path = "$TESTCASE_FILE" file_path = "$TESTCASE_FILE"
locust_tests = load_locust_tests(file_path) locust_tests = prepare_locust_tests(file_path)
config = locust_tests["config"] functions = locust_tests["functions"]
tests = locust_tests["tests"] tests = locust_tests["tests"]
config = {}
host = config.get('request', {}).get('base_url', '') host = config.get('base_url', '')
@@ -1,7 +1,6 @@
- config: - config:
name: basic test with httpbin name: basic test with httpbin
request: base_url: https://httpbin.org/
base_url: https://httpbin.org/
- test: - test:
name: index name: index
+13
View File
@@ -1,6 +1,7 @@
import os import os
import shutil import shutil
import time import time
import unittest
from httprunner import HttpRunner, api, loader, parser from httprunner import HttpRunner, api, loader, parser
from locust import HttpLocust from locust import HttpLocust
@@ -565,3 +566,15 @@ class TestApi(ApiServerUnittest):
results.records[1]["name"], results.records[1]["name"],
"create user 1001 and check result." "create user 1001 and check result."
) )
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")
-8
View File
@@ -444,11 +444,3 @@ class TestSuiteLoader(unittest.TestCase):
loader.load_project_tests(os.path.join(os.getcwd(), "tests")) loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
self.assertIn("get_token", self.tests_def_mapping["api"]) self.assertIn("get_token", self.tests_def_mapping["api"])
self.assertEqual(self.project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH") 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")