mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-11 18:11:21 +08:00
@@ -1,7 +1,7 @@
|
||||
__title__ = 'HttpRunner'
|
||||
__description__ = 'One-stop solution for HTTP(S) testing.'
|
||||
__url__ = 'https://github.com/HttpRunner/HttpRunner'
|
||||
__version__ = '1.5.11'
|
||||
__version__ = '1.5.12'
|
||||
__author__ = 'debugtalk'
|
||||
__author_email__ = 'mail@debugtalk.com'
|
||||
__license__ = 'MIT'
|
||||
|
||||
@@ -19,6 +19,7 @@ class HttpRunner(object):
|
||||
resultclass (class): HtmlTestResult or TextTestResult
|
||||
failfast (bool): False/True, stop the test run on the first error or failure.
|
||||
http_client_session (instance): requests.Session(), or locust.client.Session() instance.
|
||||
dot_env_path (str): .env file path.
|
||||
|
||||
Attributes:
|
||||
project_mapping (dict): save project loaded api/testcases, environments and debugtalk.py module.
|
||||
@@ -33,8 +34,9 @@ class HttpRunner(object):
|
||||
}
|
||||
|
||||
"""
|
||||
loader.dot_env_path = kwargs.pop("dot_env_path", None)
|
||||
self.http_client_session = kwargs.pop("http_client_session", None)
|
||||
self.kwargs = kwargs
|
||||
self.http_client_session = self.kwargs.pop("http_client_session", None)
|
||||
|
||||
def load_tests(self, path_or_testcases):
|
||||
""" load testcases, extend and merge with api/testcase definitions.
|
||||
|
||||
@@ -39,6 +39,9 @@ def main_hrun():
|
||||
parser.add_argument(
|
||||
'--log-file',
|
||||
help="Write logs to specified file path.")
|
||||
parser.add_argument(
|
||||
'--dot-env-path',
|
||||
help="Specify .env file path, which is useful for keeping sensitive data.")
|
||||
parser.add_argument(
|
||||
'--failfast', action='store_true', default=False,
|
||||
help="Stop the test run on the first error or failure.")
|
||||
@@ -75,7 +78,7 @@ def main_hrun():
|
||||
create_scaffold(project_path)
|
||||
exit(0)
|
||||
|
||||
runner = HttpRunner(failfast=args.failfast).run(args.testset_paths)
|
||||
runner = HttpRunner(failfast=args.failfast, dot_env_path=args.dot_env_path).run(args.testset_paths)
|
||||
|
||||
if not args.no_html_report:
|
||||
runner.gen_html_report(
|
||||
|
||||
@@ -230,6 +230,7 @@ class Context(object):
|
||||
|
||||
logger.log_info("start to validate.")
|
||||
validate_pass = True
|
||||
failures = []
|
||||
|
||||
for validator in validators:
|
||||
# evaluate validators with context variable mapping.
|
||||
@@ -240,12 +241,14 @@ class Context(object):
|
||||
|
||||
try:
|
||||
self._do_validation(evaluated_validator)
|
||||
except exceptions.ValidationFailure:
|
||||
except exceptions.ValidationFailure as ex:
|
||||
validate_pass = False
|
||||
failures.append(str(ex))
|
||||
|
||||
evaluated_validators.append(evaluated_validator)
|
||||
|
||||
if not validate_pass:
|
||||
raise exceptions.ValidationFailure
|
||||
failures_string = "\n".join([failure for failure in failures])
|
||||
raise exceptions.ValidationFailure(failures_string)
|
||||
|
||||
return evaluated_validators
|
||||
|
||||
@@ -24,6 +24,8 @@ project_mapping = {
|
||||
""" dict: save project loaded api/testcases definitions, environments and debugtalk.py module.
|
||||
"""
|
||||
|
||||
dot_env_path = None
|
||||
|
||||
testcases_cache_mapping = {}
|
||||
project_working_directory = os.getcwd()
|
||||
|
||||
@@ -161,7 +163,8 @@ def load_folder_files(folder_path, recursive=True):
|
||||
|
||||
|
||||
def load_dot_env_file():
|
||||
""" load .env file, .env file should be located in project working directory.
|
||||
""" load .env file, .env file should be located in project working directory by default.
|
||||
If dot_env_path is specified, it will be loaded instead.
|
||||
|
||||
Returns:
|
||||
dict: environment variables mapping
|
||||
@@ -176,10 +179,14 @@ def load_dot_env_file():
|
||||
exceptions.FileFormatError: If env file format is invalid.
|
||||
|
||||
"""
|
||||
path = os.path.join(project_working_directory, ".env")
|
||||
path = dot_env_path or os.path.join(project_working_directory, ".env")
|
||||
if not os.path.isfile(path):
|
||||
logger.log_debug(".env file not exist in : {}".format(project_working_directory))
|
||||
return {}
|
||||
if dot_env_path:
|
||||
logger.log_error(".env file not exist: {}".format(dot_env_path))
|
||||
sys.exit(1)
|
||||
else:
|
||||
logger.log_debug(".env file not exist in: {}".format(project_working_directory))
|
||||
return {}
|
||||
|
||||
logger.log_info("Loading environment variables from {}".format(path))
|
||||
env_variables_mapping = {}
|
||||
|
||||
@@ -214,8 +214,7 @@ class Runner(object):
|
||||
# validate
|
||||
try:
|
||||
self.evaluated_validators = self.context.validate(validators, resp_obj)
|
||||
except (exceptions.ParamsError, \
|
||||
exceptions.ValidationFailure, exceptions.ExtractFailure):
|
||||
except (exceptions.ParamsError, exceptions.ValidationFailure, exceptions.ExtractFailure):
|
||||
# log request
|
||||
err_req_msg = "request: \n"
|
||||
err_req_msg += "headers: {}\n".format(parsed_request.pop("headers", {}))
|
||||
|
||||
@@ -263,7 +263,7 @@ def add_teststep(test_runner, teststep_dict):
|
||||
try:
|
||||
test_runner.run_test(teststep_dict)
|
||||
except exceptions.MyBaseFailure as ex:
|
||||
self.fail(repr(ex))
|
||||
self.fail(str(ex))
|
||||
finally:
|
||||
if hasattr(test_runner.http_client_session, "meta_data"):
|
||||
self.meta_data = test_runner.http_client_session.meta_data
|
||||
|
||||
3
tests/data/test.env
Normal file
3
tests/data/test.env
Normal file
@@ -0,0 +1,3 @@
|
||||
UserName=test
|
||||
Password=654321
|
||||
PROJECT_KEY=AAABBBCCC
|
||||
@@ -140,6 +140,16 @@ class TestFileLoader(unittest.TestCase):
|
||||
self.assertIn("PROJECT_KEY", env_variables_mapping)
|
||||
self.assertEqual(env_variables_mapping["UserName"], "debugtalk")
|
||||
|
||||
def test_load_custom_dot_env_file(self):
|
||||
loader.project_working_directory = os.path.join(
|
||||
os.getcwd(), "tests",
|
||||
)
|
||||
loader.dot_env_path = "tests/data/test.env"
|
||||
env_variables_mapping = loader.load_dot_env_file()
|
||||
self.assertIn("PROJECT_KEY", env_variables_mapping)
|
||||
self.assertEqual(env_variables_mapping["UserName"], "test")
|
||||
loader.dot_env_path = None
|
||||
|
||||
def test_load_env_path_not_exist(self):
|
||||
loader.project_working_directory = os.path.join(
|
||||
os.getcwd(), "tests", "data",
|
||||
|
||||
Reference in New Issue
Block a user