From d65f3860b19dc1a39da8ad52cc09515206b18b22 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 19 Sep 2019 00:22:18 +0800 Subject: [PATCH] feat: config variables support parsing from function --- CHANGELOG.md | 11 +++++++++++ httprunner/__init__.py | 2 +- httprunner/parser.py | 15 +++++++++++++- pyproject.toml | 2 +- tests/debugtalk.py | 7 +++++++ tests/test_parser.py | 2 -- tests/test_runner.py | 45 +++++++++++++++++++++++++++++++++++++++++- 7 files changed, 78 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccea9762..9d2b70c5 100644 --- a/CHANGELOG.md +++ b/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** diff --git a/httprunner/__init__.py b/httprunner/__init__.py index 567610eb..7fbb6af4 100644 --- a/httprunner/__init__.py +++ b/httprunner/__init__.py @@ -1,4 +1,4 @@ -__version__ = "2.2.5" +__version__ = "2.2.6" __description__ = "One-stop solution for HTTP(S) testing." __all__ = ["__version__", "__description__"] diff --git a/httprunner/parser.py b/httprunner/parser.py index a2147a52..29b0a666 100644 --- a/httprunner/parser.py +++ b/httprunner/parser.py @@ -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) diff --git a/pyproject.toml b/pyproject.toml index 89c2f95e..c7e8f540 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/tests/debugtalk.py b/tests/debugtalk.py index 74b9a3d3..dd89e8be 100644 --- a/tests/debugtalk.py +++ b/tests/debugtalk.py @@ -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 + } diff --git a/tests/test_parser.py b/tests/test_parser.py index 5391ebc2..6f76148e 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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 } diff --git a/tests/test_runner.py b/tests/test_runner.py index 4b684c1d..df8c734d 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -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]) \ No newline at end of file + 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")