mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-13 04:30:41 +08:00
feat: config variables support parsing from function
This commit is contained in:
11
CHANGELOG.md
11
CHANGELOG.md
@@ -1,5 +1,16 @@
|
||||
# Release History
|
||||
|
||||
## 2.2.6 (2019-09-18)
|
||||
|
||||
**Added**
|
||||
|
||||
- config variables support parsing from function
|
||||
|
||||
**Changed**
|
||||
|
||||
- remove unused import
|
||||
- adjust code format
|
||||
|
||||
## 2.2.5 (2019-07-28)
|
||||
|
||||
**Added**
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
__version__ = "2.2.5"
|
||||
__version__ = "2.2.6"
|
||||
__description__ = "One-stop solution for HTTP(S) testing."
|
||||
|
||||
__all__ = ["__version__", "__description__"]
|
||||
|
||||
@@ -871,10 +871,23 @@ def __prepare_config(config, project_mapping, session_variables_set=None):
|
||||
"""
|
||||
# get config variables
|
||||
raw_config_variables = config.pop("variables", {})
|
||||
raw_config_variables_mapping = utils.ensure_mapping_format(raw_config_variables)
|
||||
|
||||
override_variables = utils.deepcopy_dict(project_mapping.get("variables", {}))
|
||||
functions = project_mapping.get("functions", {})
|
||||
|
||||
if isinstance(raw_config_variables, basestring) and function_regex_compile.match(raw_config_variables):
|
||||
# config variables are generated by calling function
|
||||
# e.g.
|
||||
# "config": {
|
||||
# "name": "basic test with httpbin",
|
||||
# "variables": "${gen_variables()}"
|
||||
# }
|
||||
raw_config_variables_mapping = parse_lazy_data(
|
||||
prepare_lazy_data(raw_config_variables, functions_mapping=functions)
|
||||
)
|
||||
else:
|
||||
raw_config_variables_mapping = utils.ensure_mapping_format(raw_config_variables)
|
||||
|
||||
# override config variables with passed in variables
|
||||
raw_config_variables_mapping.update(override_variables)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "httprunner"
|
||||
version = "2.2.5"
|
||||
version = "2.2.6"
|
||||
description = "One-stop solution for HTTP(S) testing."
|
||||
license = "Apache-2.0"
|
||||
readme = "README.md"
|
||||
|
||||
@@ -111,3 +111,10 @@ def alter_response(response):
|
||||
def alter_response_error(response):
|
||||
# NameError
|
||||
not_defined_variable
|
||||
|
||||
|
||||
def gen_variables():
|
||||
return {
|
||||
"var_a": 1,
|
||||
"var_b": 2
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import unittest
|
||||
|
||||
@@ -561,7 +560,6 @@ class TestParserBasic(unittest.TestCase):
|
||||
)
|
||||
|
||||
def test_parse_data_functions(self):
|
||||
import random, string
|
||||
functions_mapping = {
|
||||
"gen_random_string": gen_random_string
|
||||
}
|
||||
|
||||
@@ -336,4 +336,47 @@ class TestRunner(ApiServerUnittest):
|
||||
parsed_testcases = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_testcases[0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
|
||||
def test_run_testcase_config_variables_parsed_from_function(self):
|
||||
testcases = [
|
||||
{
|
||||
"config": {
|
||||
"name": "basic test with httpbin",
|
||||
"base_url": HTTPBIN_SERVER,
|
||||
"variables": "${gen_variables()}"
|
||||
},
|
||||
"teststeps": [
|
||||
{
|
||||
"name": "modify request headers",
|
||||
"base_url": HTTPBIN_SERVER,
|
||||
"request": {
|
||||
"url": "/anything",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"user_agent": "iOS/10.3",
|
||||
"os_platform": "ios"
|
||||
},
|
||||
"data": "a=1&b=2"
|
||||
},
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
tests_mapping = {
|
||||
"project_mapping": {
|
||||
"functions": self.debugtalk_functions
|
||||
},
|
||||
"testcases": testcases
|
||||
}
|
||||
parsed_testcases = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_testcases[0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
test_variables_mapping = test_runner.session_context.test_variables_mapping
|
||||
self.assertEqual(test_variables_mapping["var_a"], 1)
|
||||
self.assertEqual(test_variables_mapping["var_b"], 2)
|
||||
self.assertEqual(test_variables_mapping["request"]["data"], "a=1&b=2")
|
||||
|
||||
Reference in New Issue
Block a user