call load_dot_env_file when initializing HttpRunner

This commit is contained in:
debugtalk
2018-05-09 17:16:20 +08:00
parent 9c26dba415
commit a414fe56d5
6 changed files with 30 additions and 10 deletions

View File

@@ -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'

View File

@@ -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(

View File

@@ -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)

View File

@@ -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:

3
tests/data/test.env Normal file
View File

@@ -0,0 +1,3 @@
UserName=debugtalk
Password=123456
PROJECT_KEY=ABCDEFGH

View File

@@ -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)