From 777c144813d5e16fc16d23993f6a2c8b90d9aa25 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 28 Aug 2018 14:35:26 +0800 Subject: [PATCH 1/2] add --dot-env-path back --- httprunner/api.py | 4 +++- httprunner/cli.py | 5 ++++- httprunner/loader.py | 15 +++++++++++---- tests/data/test.env | 3 +++ tests/test_loader.py | 10 ++++++++++ 5 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 tests/data/test.env diff --git a/httprunner/api.py b/httprunner/api.py index 2e87c5fe..b3356732 100644 --- a/httprunner/api.py +++ b/httprunner/api.py @@ -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. diff --git a/httprunner/cli.py b/httprunner/cli.py index fc15ab5b..ec645471 100644 --- a/httprunner/cli.py +++ b/httprunner/cli.py @@ -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( diff --git a/httprunner/loader.py b/httprunner/loader.py index e17c46d4..ae6e5a85 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -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 = {} diff --git a/tests/data/test.env b/tests/data/test.env new file mode 100644 index 00000000..09816d83 --- /dev/null +++ b/tests/data/test.env @@ -0,0 +1,3 @@ +UserName=test +Password=654321 +PROJECT_KEY=AAABBBCCC \ No newline at end of file diff --git a/tests/test_loader.py b/tests/test_loader.py index b6f08745..18e67d86 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -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", From a4b0a06a06966e40dd800b6b7ee02dcf35d7cdca Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 29 Aug 2018 00:00:02 +0800 Subject: [PATCH 2/2] bugfix: lost ValidationFailure message --- httprunner/__about__.py | 2 +- httprunner/context.py | 7 +++++-- httprunner/runner.py | 3 +-- httprunner/utils.py | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/httprunner/__about__.py b/httprunner/__about__.py index 4a8bceb5..30d339cc 100644 --- a/httprunner/__about__.py +++ b/httprunner/__about__.py @@ -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' diff --git a/httprunner/context.py b/httprunner/context.py index 766aae22..24fdbab2 100644 --- a/httprunner/context.py +++ b/httprunner/context.py @@ -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 diff --git a/httprunner/runner.py b/httprunner/runner.py index 7342a89a..16271b4d 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -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", {})) diff --git a/httprunner/utils.py b/httprunner/utils.py index be0c6b75..1b037ec3 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -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