relocate loader functions in HttpRunner class

This commit is contained in:
httprunner
2018-08-12 16:33:21 +08:00
parent d21dd69eaf
commit e40791dac5
2 changed files with 32 additions and 14 deletions

View File

@@ -880,19 +880,16 @@ def load_test_folder(test_folder_path=None):
return test_definition_mapping return test_definition_mapping
def load_project_tests(folder_path=None): def load_project_tests(folder_path):
""" load api, testcases and debugtalk.py module. """ load api, testcases and debugtalk.py module.
Args: Args:
folder_path (str): folder path. folder_path (str): folder path.
If not set, defautls to current working directory.
Returns: Returns:
dict: project tests mapping. dict: project tests mapping.
""" """
folder_path = folder_path or os.getcwd()
load_debugtalk_module(folder_path) load_debugtalk_module(folder_path)
load_api_folder(os.path.join(folder_path, "api")) load_api_folder(os.path.join(folder_path, "api"))
load_test_folder(os.path.join(folder_path, "suite")) load_test_folder(os.path.join(folder_path, "suite"))

View File

@@ -1,6 +1,7 @@
# encoding: utf-8 # encoding: utf-8
import copy import copy
import os
import sys import sys
import unittest import unittest
@@ -83,13 +84,13 @@ class TestSuite(unittest.TestSuite):
teststeps = testcase.get("teststeps", []) teststeps = testcase.get("teststeps", [])
for config_variables in config_parametered_variables_list: for config_variables in config_parametered_variables_list:
# config level # testcase config level
self.config["variables"] = config_variables self.config["variables"] = config_variables
test_runner = runner.Runner(self.config, http_client_session) test_runner = runner.Runner(self.config, http_client_session)
for teststep_dict in teststeps: for teststep_dict in teststeps:
teststep_dict = copy.copy(teststep_dict) teststep_dict = copy.copy(teststep_dict)
# testcase level # teststep level
testcase_parametered_variables_list = self._get_parametered_variables( testcase_parametered_variables_list = self._get_parametered_variables(
teststep_dict.get("variables", []), teststep_dict.get("variables", []),
teststep_dict.get("parameters", []) teststep_dict.get("parameters", [])
@@ -97,7 +98,7 @@ class TestSuite(unittest.TestSuite):
for testcase_variables in testcase_parametered_variables_list: for testcase_variables in testcase_parametered_variables_list:
teststep_dict["variables"] = testcase_variables teststep_dict["variables"] = testcase_variables
# eval testcase name with bind variables # eval teststep name with bind variables
variables = utils.override_variables_binds( variables = utils.override_variables_binds(
config_variables, config_variables,
testcase_variables testcase_variables
@@ -106,14 +107,14 @@ class TestSuite(unittest.TestSuite):
try: try:
testcase_name = self.testcase_parser.eval_content_with_bindings(teststep_dict["name"]) testcase_name = self.testcase_parser.eval_content_with_bindings(teststep_dict["name"])
except (AssertionError, exceptions.ParamsError): except (AssertionError, exceptions.ParamsError):
logger.log_warning("failed to eval testcase name: {}".format(teststep_dict["name"])) logger.log_warning("failed to eval teststep name: {}".format(teststep_dict["name"]))
testcase_name = teststep_dict["name"] testcase_name = teststep_dict["name"]
self.test_runner_list.append((test_runner, variables)) self.test_runner_list.append((test_runner, variables))
self._add_test_to_suite(testcase_name, test_runner, teststep_dict) self._add_test_to_suite(testcase_name, test_runner, teststep_dict)
def _get_parametered_variables(self, variables, parameters): def _get_parametered_variables(self, variables, parameters):
""" parameterize varaibles with parameters """ parameterize variables with parameters
""" """
cartesian_product_parameters = context.parse_parameters( cartesian_product_parameters = context.parse_parameters(
parameters, parameters,
@@ -224,16 +225,36 @@ class HttpRunner(object):
""" """
dot_env_path = kwargs.pop("dot_env_path", None) dot_env_path = kwargs.pop("dot_env_path", None)
loader.load_dot_env_file(dot_env_path) self.project_mapping = self.loader(dot_env_path)
loader.load_project_tests("tests") # TODO: remove tests
self.project_mapping = loader.project_mapping
utils.set_os_environ(self.project_mapping["env"])
kwargs.setdefault("resultclass", HtmlTestResult) kwargs.setdefault("resultclass", HtmlTestResult)
self.runner = unittest.TextTestRunner(**kwargs) self.runner = unittest.TextTestRunner(**kwargs)
def loader(self, dot_env_path=None):
""" load project files, including api/testcase definitions, testcases,
environment variables and debugtalk.py module.
Args:
dot_env_path (str): .env file path
Returns:
dict: project tests info mapping.
"""
# load .env
loader.load_dot_env_file(dot_env_path)
# load api/testcase definition and debugtalk.py module
project_folder_path = os.path.join(os.getcwd(), "tests") # TODO: remove tests
loader.load_project_tests(project_folder_path)
project_mapping = loader.project_mapping
utils.set_os_environ(project_mapping["env"])
return project_mapping
def run(self, path_or_testcases, mapping=None): def run(self, path_or_testcases, mapping=None):
""" start to run test with varaibles mapping. """ start to run test with variables mapping.
Args: Args:
path_or_testcases (str/list/dict): YAML/JSON testcase file path or testcase list path_or_testcases (str/list/dict): YAML/JSON testcase file path or testcase list