feat: cache loading project meta

This commit is contained in:
debugtalk
2020-05-15 11:13:53 +08:00
parent 7896a57a72
commit 46d3793244
3 changed files with 19 additions and 14 deletions
+15 -10
View File
@@ -22,6 +22,9 @@ except AttributeError:
pass pass
project_meta_cached_mapping: Dict[str, ProjectMeta] = {}
def _load_yaml_file(yaml_file): def _load_yaml_file(yaml_file):
""" load yaml file and check file content format """ load yaml file and check file content format
""" """
@@ -281,7 +284,6 @@ def init_project_working_directory(test_path):
# locate debugtalk.py file # locate debugtalk.py file
debugtalk_path = locate_debugtalk_py(test_path) debugtalk_path = locate_debugtalk_py(test_path)
global project_working_directory
if debugtalk_path: if debugtalk_path:
# The folder contains debugtalk.py will be treated as PWD. # The folder contains debugtalk.py will be treated as PWD.
project_working_directory = os.path.dirname(debugtalk_path) project_working_directory = os.path.dirname(debugtalk_path)
@@ -312,31 +314,33 @@ def load_debugtalk_functions():
return load_module_functions(imported_module) return load_module_functions(imported_module)
def load_project_data(test_path: str, dot_env_path: str = None) -> ProjectMeta: def load_project_meta(test_path: str) -> ProjectMeta:
""" load api, testcases, .env, debugtalk.py functions. """ load api, testcases, .env, debugtalk.py functions.
api/testcases folder is relative to project_working_directory api/testcases folder is relative to project_working_directory
Args: Args:
test_path (str): test file/folder path, locate pwd from this path. test_path (str): test file/folder path, locate pwd from this path.
dot_env_path (str): specified .env file path
Returns: Returns:
project loaded api/testcases definitions, project loaded api/testcases definitions,
environments and debugtalk.py functions. environments and debugtalk.py functions.
""" """
if test_path in project_meta_cached_mapping:
return project_meta_cached_mapping[test_path]
debugtalk_path, project_working_directory = init_project_working_directory( debugtalk_path, project_working_directory = init_project_working_directory(
test_path test_path
) )
project_meta = {} project_meta = ProjectMeta()
# load .env file # load .env file
# NOTICE: # NOTICE:
# environment variable maybe loaded in debugtalk.py # environment variable maybe loaded in debugtalk.py
# thus .env file should be loaded before loading debugtalk.py # thus .env file should be loaded before loading debugtalk.py
dot_env_path = dot_env_path or os.path.join(project_working_directory, ".env") dot_env_path = os.path.join(project_working_directory, ".env")
project_meta["env"] = load_dot_env_file(dot_env_path) project_meta.env = load_dot_env_file(dot_env_path)
if debugtalk_path: if debugtalk_path:
# load debugtalk.py functions # load debugtalk.py functions
@@ -345,10 +349,11 @@ def load_project_data(test_path: str, dot_env_path: str = None) -> ProjectMeta:
debugtalk_functions = {} debugtalk_functions = {}
# locate PWD and load debugtalk.py functions # locate PWD and load debugtalk.py functions
project_meta["PWD"] = project_working_directory project_meta.PWD = project_working_directory
project_meta["functions"] = debugtalk_functions project_meta.functions = debugtalk_functions
project_meta["test_path"] = os.path.abspath(test_path)[ project_meta.test_path = os.path.abspath(test_path)[
len(project_working_directory) + 1 : len(project_working_directory) + 1 :
] ]
return ProjectMeta.parse_obj(project_meta) project_meta_cached_mapping[test_path] = project_meta
return project_meta
+2 -2
View File
@@ -6,7 +6,7 @@ from loguru import logger
from httprunner import utils, exceptions from httprunner import utils, exceptions
from httprunner.client import HttpSession from httprunner.client import HttpSession
from httprunner.exceptions import ValidationFailure, ParamsError from httprunner.exceptions import ValidationFailure, ParamsError
from httprunner.new_loader import load_project_data, load_testcase_file from httprunner.new_loader import load_project_meta, load_testcase_file
from httprunner.parser import build_url, parse_data, parse_variables_mapping from httprunner.parser import build_url, parse_data, parse_variables_mapping
from httprunner.response import ResponseObject from httprunner.response import ResponseObject
from httprunner.schema import ( from httprunner.schema import (
@@ -35,7 +35,7 @@ class HttpRunner(object):
self.session_variables: Dict = {} self.session_variables: Dict = {}
self.success: bool = True # indicate testcase execution result self.success: bool = True # indicate testcase execution result
self.project_data = load_project_data(self.config.path) self.project_data = load_project_meta(self.config.path)
self.config.functions = self.project_data.functions self.config.functions = self.project_data.functions
def with_variables(self, **variables: VariablesMapping) -> "HttpRunner": def with_variables(self, **variables: VariablesMapping) -> "HttpRunner":
+2 -2
View File
@@ -77,8 +77,8 @@ class ProjectMeta(BaseModel):
variables: VariablesMapping = {} variables: VariablesMapping = {}
functions: FunctionsMapping = {} functions: FunctionsMapping = {}
env: Env = {} env: Env = {}
PWD: Text PWD: Text = None
test_path: Text test_path: Text = None # run with specified test path
class TestsMapping(BaseModel): class TestsMapping(BaseModel):