mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 06:23:52 +08:00
call load_dot_env_file when initializing HttpRunner
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
__title__ = 'HttpRunner'
|
__title__ = 'HttpRunner'
|
||||||
__description__ = 'One-stop solution for HTTP(S) testing.'
|
__description__ = 'One-stop solution for HTTP(S) testing.'
|
||||||
__url__ = 'https://github.com/HttpRunner/HttpRunner'
|
__url__ = 'https://github.com/HttpRunner/HttpRunner'
|
||||||
__version__ = '1.4.1'
|
__version__ = '1.4.2'
|
||||||
__author__ = 'debugtalk'
|
__author__ = 'debugtalk'
|
||||||
__author_email__ = 'mail@debugtalk.com'
|
__author_email__ = 'mail@debugtalk.com'
|
||||||
__license__ = 'MIT'
|
__license__ = 'MIT'
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ from httprunner.__about__ import __description__, __version__
|
|||||||
from httprunner.compat import is_py2
|
from httprunner.compat import is_py2
|
||||||
from httprunner.task import HttpRunner
|
from httprunner.task import HttpRunner
|
||||||
from httprunner.utils import (create_scaffold, get_python2_retire_msg,
|
from httprunner.utils import (create_scaffold, get_python2_retire_msg,
|
||||||
load_dot_env_file, prettify_json_file,
|
prettify_json_file, print_output,
|
||||||
print_output, validate_json_file)
|
validate_json_file)
|
||||||
|
|
||||||
|
|
||||||
def main_hrun():
|
def main_hrun():
|
||||||
@@ -73,17 +73,13 @@ def main_hrun():
|
|||||||
prettify_json_file(args.prettify)
|
prettify_json_file(args.prettify)
|
||||||
exit(0)
|
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
|
project_name = args.startproject
|
||||||
if project_name:
|
if project_name:
|
||||||
project_path = os.path.join(os.getcwd(), project_name)
|
project_path = os.path.join(os.getcwd(), project_name)
|
||||||
create_scaffold(project_path)
|
create_scaffold(project_path)
|
||||||
exit(0)
|
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:
|
if not args.no_html_report:
|
||||||
runner.gen_html_report(
|
runner.gen_html_report(
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from httprunner import exception, logger, runner, testcase, utils
|
|||||||
from httprunner.compat import is_py3
|
from httprunner.compat import is_py3
|
||||||
from httprunner.report import HtmlTestResult, get_summary, render_html_report
|
from httprunner.report import HtmlTestResult, get_summary, render_html_report
|
||||||
from httprunner.testcase import TestcaseLoader
|
from httprunner.testcase import TestcaseLoader
|
||||||
|
from httprunner.utils import load_dot_env_file
|
||||||
|
|
||||||
|
|
||||||
class TestCase(unittest.TestCase):
|
class TestCase(unittest.TestCase):
|
||||||
@@ -212,7 +213,11 @@ class HttpRunner(object):
|
|||||||
@param (dict) kwargs: key-value arguments used to initialize TextTestRunner
|
@param (dict) kwargs: key-value arguments used to initialize TextTestRunner
|
||||||
- resultclass: HtmlTestResult or TextTestResult
|
- resultclass: HtmlTestResult or TextTestResult
|
||||||
- failfast: False/True, stop the test run on the first error or failure.
|
- 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)
|
kwargs.setdefault("resultclass", HtmlTestResult)
|
||||||
self.runner = unittest.TextTestRunner(**kwargs)
|
self.runner = unittest.TextTestRunner(**kwargs)
|
||||||
|
|
||||||
|
|||||||
@@ -500,8 +500,14 @@ def create_scaffold(project_path):
|
|||||||
def load_dot_env_file(path):
|
def load_dot_env_file(path):
|
||||||
""" load .env file and set to os.environ
|
""" load .env file and set to os.environ
|
||||||
"""
|
"""
|
||||||
if not os.path.isfile(path):
|
if not path:
|
||||||
return
|
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))
|
logger.log_info("Loading environment variables from {}".format(path))
|
||||||
with io.open(path, 'r', encoding='utf-8') as fp:
|
with io.open(path, 'r', encoding='utf-8') as fp:
|
||||||
|
|||||||
3
tests/data/test.env
Normal file
3
tests/data/test.env
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
UserName=debugtalk
|
||||||
|
Password=123456
|
||||||
|
PROJECT_KEY=ABCDEFGH
|
||||||
@@ -2,6 +2,7 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from httprunner import HttpRunner
|
from httprunner import HttpRunner
|
||||||
|
from httprunner.exception import FileNotFoundError
|
||||||
from tests.base import ApiServerUnittest
|
from tests.base import ApiServerUnittest
|
||||||
|
|
||||||
|
|
||||||
@@ -152,3 +153,12 @@ class TestHttpRunner(ApiServerUnittest):
|
|||||||
summary = runner.summary
|
summary = runner.summary
|
||||||
self.assertTrue(summary["success"])
|
self.assertTrue(summary["success"])
|
||||||
self.assertEqual(summary["stat"]["testsRun"], 8)
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user