From a414fe56d55db3d0173874c13e691d669dc441c7 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 9 May 2018 17:16:20 +0800 Subject: [PATCH] call load_dot_env_file when initializing HttpRunner --- httprunner/__about__.py | 2 +- httprunner/cli.py | 10 +++------- httprunner/task.py | 5 +++++ httprunner/utils.py | 10 ++++++++-- tests/data/test.env | 3 +++ tests/test_httprunner.py | 10 ++++++++++ 6 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 tests/data/test.env diff --git a/httprunner/__about__.py b/httprunner/__about__.py index 803689bf..bf3be10f 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.4.1' +__version__ = '1.4.2' __author__ = 'debugtalk' __author_email__ = 'mail@debugtalk.com' __license__ = 'MIT' diff --git a/httprunner/cli.py b/httprunner/cli.py index 7814de91..aeb74c2b 100644 --- a/httprunner/cli.py +++ b/httprunner/cli.py @@ -11,8 +11,8 @@ from httprunner.__about__ import __description__, __version__ from httprunner.compat import is_py2 from httprunner.task import HttpRunner from httprunner.utils import (create_scaffold, get_python2_retire_msg, - load_dot_env_file, prettify_json_file, - print_output, validate_json_file) + prettify_json_file, print_output, + validate_json_file) def main_hrun(): @@ -73,17 +73,13 @@ def main_hrun(): prettify_json_file(args.prettify) exit(0) - dot_env_path = args.dot_env_path or os.path.join(os.getcwd(), ".env") - if dot_env_path: - load_dot_env_file(dot_env_path) - project_name = args.startproject if project_name: project_path = os.path.join(os.getcwd(), project_name) 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/task.py b/httprunner/task.py index 6cb728b5..1757c315 100644 --- a/httprunner/task.py +++ b/httprunner/task.py @@ -8,6 +8,7 @@ from httprunner import exception, logger, runner, testcase, utils from httprunner.compat import is_py3 from httprunner.report import HtmlTestResult, get_summary, render_html_report from httprunner.testcase import TestcaseLoader +from httprunner.utils import load_dot_env_file class TestCase(unittest.TestCase): @@ -212,7 +213,11 @@ class HttpRunner(object): @param (dict) kwargs: key-value arguments used to initialize TextTestRunner - resultclass: HtmlTestResult or TextTestResult - failfast: False/True, stop the test run on the first error or failure. + - dot_env_path: .env file path """ + dot_env_path = kwargs.pop("dot_env_path", None) + load_dot_env_file(dot_env_path) + kwargs.setdefault("resultclass", HtmlTestResult) self.runner = unittest.TextTestRunner(**kwargs) diff --git a/httprunner/utils.py b/httprunner/utils.py index d6c96c85..ea079540 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -500,8 +500,14 @@ def create_scaffold(project_path): def load_dot_env_file(path): """ load .env file and set to os.environ """ - if not os.path.isfile(path): - return + if not path: + path = os.path.join(os.getcwd(), ".env") + if not os.path.isfile(path): + logger.log_debug(".env file not exist: {}".format(path)) + return + else: + if not os.path.isfile(path): + raise exception.FileNotFoundError("env file not exist: {}".format(path)) logger.log_info("Loading environment variables from {}".format(path)) with io.open(path, 'r', encoding='utf-8') as fp: diff --git a/tests/data/test.env b/tests/data/test.env new file mode 100644 index 00000000..6bb8d3c3 --- /dev/null +++ b/tests/data/test.env @@ -0,0 +1,3 @@ +UserName=debugtalk +Password=123456 +PROJECT_KEY=ABCDEFGH \ No newline at end of file diff --git a/tests/test_httprunner.py b/tests/test_httprunner.py index a3d584c4..c3b9d6a7 100644 --- a/tests/test_httprunner.py +++ b/tests/test_httprunner.py @@ -2,6 +2,7 @@ import os import shutil from httprunner import HttpRunner +from httprunner.exception import FileNotFoundError from tests.base import ApiServerUnittest @@ -152,3 +153,12 @@ class TestHttpRunner(ApiServerUnittest): summary = runner.summary self.assertTrue(summary["success"]) self.assertEqual(summary["stat"]["testsRun"], 8) + + def test_load_env_path(self): + self.assertNotIn("PROJECT_KEY", os.environ) + HttpRunner(dot_env_path="tests/data/test.env").run(self.testset_path) + self.assertIn("PROJECT_KEY", os.environ) + + def test_load_env_path_not_exist(self): + with self.assertRaises(FileNotFoundError): + HttpRunner(dot_env_path="not_exist.env").run(self.testset_path)