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

View File

@@ -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
}

View File

@@ -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
}

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", {})

View File

@@ -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', '')

View File

@@ -1,7 +1,6 @@
- config:
name: basic test with httpbin
request:
base_url: https://httpbin.org/
base_url: https://httpbin.org/
- test:
name: index

View File

@@ -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."
)
)
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")

View File

@@ -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")