diff --git a/.gitignore b/.gitignore index 31a72c46..0487b44f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ logs/% .coverage locustfile.py site/ +.env \ No newline at end of file diff --git a/httprunner/__about__.py b/httprunner/__about__.py index 1a85421e..f401e934 100644 --- a/httprunner/__about__.py +++ b/httprunner/__about__.py @@ -1,7 +1,7 @@ __title__ = 'HttpRunner' __description__ = 'HTTP test runner, not just about api test and load test.' __url__ = 'https://github.com/HttpRunner/HttpRunner' -__version__ = '0.9.6a2' +__version__ = '0.9.7' __author__ = 'debugtalk' __author_email__ = 'mail@debugtalk.com' __license__ = 'MIT' diff --git a/httprunner/cli.py b/httprunner/cli.py index 2b2e1c5c..ee0fb400 100644 --- a/httprunner/cli.py +++ b/httprunner/cli.py @@ -7,7 +7,8 @@ import unittest from httprunner import logger from httprunner.__about__ import __version__ from httprunner.task import HttpRunner -from httprunner.utils import create_scaffold, print_output, string_type +from httprunner.utils import (create_scaffold, load_dot_env_file, print_output, + string_type) def main_hrun(): @@ -30,6 +31,9 @@ def main_hrun(): parser.add_argument( '--log-level', default='INFO', help="Specify logging level, default is INFO.") + parser.add_argument( + '--dot-env-path', + help="Specify .env file path, which is useful for keeping production credentials.") parser.add_argument( '--failfast', action='store_true', default=False, help="Stop the test run on the first error or failure.") @@ -44,6 +48,10 @@ def main_hrun(): logger.color_print("{}".format(__version__), "GREEN") 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) diff --git a/httprunner/utils.py b/httprunner/utils.py index bed9af5c..9afabc6b 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -2,6 +2,7 @@ import hashlib import hmac import imp import importlib +import io import os.path import random import re @@ -390,3 +391,16 @@ def create_scaffold(project_path): msg += create_path(p[0], p[1]) logger.color_print(msg, "BLUE") + +def load_dot_env_file(path): + """ load .env file and set to os.environ + """ + if not os.path.isfile(path): + return + + logger.log_info("Loading environment variables from {}".format(path)) + with io.open(path, 'r', encoding='utf-8') as fp: + for line in fp: + variable, value = line.split("=") + os.environ[variable] = value + logger.log_debug("Loaded variable: {}".format(variable))