mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-10 23:12:41 +08:00
fix environment variable invoke:
environment variable maybe loaded in debugtalk.py thus .env file should be loaded before loading debugtalk.py
This commit is contained in:
@@ -509,40 +509,23 @@ def load_api_folder(api_folder_path):
|
|||||||
return api_definition_mapping
|
return api_definition_mapping
|
||||||
|
|
||||||
|
|
||||||
def load_debugtalk_py(start_path):
|
def locate_debugtalk_py(start_path):
|
||||||
""" locate debugtalk.py file and returns PWD and debugtalk.py functions.
|
""" locate debugtalk.py file
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
start_path (str): start locating path, maybe testcase file path or directory path
|
start_path (str): start locating path, maybe testcase file path or directory path
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
tuple: (project_working_directory, debugtalk_functions)
|
str: debugtalk.py file path, None if not found
|
||||||
|
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# locate debugtalk.py file.
|
# locate debugtalk.py file.
|
||||||
debugtalk_path = locate_file(start_path, "debugtalk.py")
|
debugtalk_path = locate_file(start_path, "debugtalk.py")
|
||||||
|
|
||||||
# The folder contains debugtalk.py will be treated as PWD.
|
|
||||||
project_working_directory = os.path.dirname(debugtalk_path)
|
|
||||||
|
|
||||||
# add PWD to sys.path
|
|
||||||
sys.path.insert(0, project_working_directory)
|
|
||||||
|
|
||||||
# load debugtalk.py functions
|
|
||||||
debugtalk_functions = load_debugtalk_functions()
|
|
||||||
|
|
||||||
except exceptions.FileNotFound:
|
except exceptions.FileNotFound:
|
||||||
|
debugtalk_path = None
|
||||||
|
|
||||||
# debugtalk.py not found, use os.getcwd() as PWD.
|
return debugtalk_path
|
||||||
project_working_directory = os.getcwd()
|
|
||||||
|
|
||||||
# add PWD to sys.path
|
|
||||||
sys.path.insert(0, project_working_directory)
|
|
||||||
|
|
||||||
debugtalk_functions = {}
|
|
||||||
|
|
||||||
return project_working_directory, debugtalk_functions
|
|
||||||
|
|
||||||
|
|
||||||
def load_project_tests(test_path, dot_env_path=None):
|
def load_project_tests(test_path, dot_env_path=None):
|
||||||
@@ -557,15 +540,37 @@ def load_project_tests(test_path, dot_env_path=None):
|
|||||||
dict: project loaded api/testcases definitions, environments and debugtalk.py functions.
|
dict: project loaded api/testcases definitions, environments and debugtalk.py functions.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# locate PWD and load debugtalk.py functions
|
# locate debugtalk.py file
|
||||||
project_working_directory, debugtalk_functions = load_debugtalk_py(test_path)
|
debugtalk_path = locate_debugtalk_py(test_path)
|
||||||
project_mapping["PWD"] = project_working_directory
|
|
||||||
project_mapping["functions"] = debugtalk_functions
|
|
||||||
|
|
||||||
# load .env
|
if debugtalk_path:
|
||||||
|
# The folder contains debugtalk.py will be treated as PWD.
|
||||||
|
project_working_directory = os.path.dirname(debugtalk_path)
|
||||||
|
else:
|
||||||
|
# debugtalk.py not found, use os.getcwd() as PWD.
|
||||||
|
project_working_directory = os.getcwd()
|
||||||
|
|
||||||
|
# add PWD to sys.path
|
||||||
|
sys.path.insert(0, project_working_directory)
|
||||||
|
|
||||||
|
# load .env file
|
||||||
|
# NOTICE:
|
||||||
|
# environment variable maybe loaded in debugtalk.py
|
||||||
|
# thus .env file should be loaded before loading debugtalk.py
|
||||||
dot_env_path = dot_env_path or os.path.join(project_working_directory, ".env")
|
dot_env_path = dot_env_path or os.path.join(project_working_directory, ".env")
|
||||||
project_mapping["env"] = load_dot_env_file(dot_env_path)
|
project_mapping["env"] = load_dot_env_file(dot_env_path)
|
||||||
|
|
||||||
|
if debugtalk_path:
|
||||||
|
# load debugtalk.py functions
|
||||||
|
debugtalk_functions = load_debugtalk_functions()
|
||||||
|
else:
|
||||||
|
debugtalk_functions = {}
|
||||||
|
|
||||||
|
# locate PWD and load debugtalk.py functions
|
||||||
|
|
||||||
|
project_mapping["PWD"] = project_working_directory
|
||||||
|
project_mapping["functions"] = debugtalk_functions
|
||||||
|
|
||||||
# load api
|
# load api
|
||||||
tests_def_mapping["api"] = load_api_folder(os.path.join(project_working_directory, "api"))
|
tests_def_mapping["api"] = load_api_folder(os.path.join(project_working_directory, "api"))
|
||||||
|
|
||||||
|
|||||||
@@ -208,21 +208,27 @@ class TestModuleLoader(unittest.TestCase):
|
|||||||
self.assertFalse(is_status_code_200(500))
|
self.assertFalse(is_status_code_200(500))
|
||||||
|
|
||||||
def test_load_debugtalk_py(self):
|
def test_load_debugtalk_py(self):
|
||||||
project_working_directory, debugtalk_functions = loader.load_debugtalk_py("tests/data/demo_testcase.yml")
|
loader.load_project_tests("tests/data/demo_testcase.yml")
|
||||||
|
project_working_directory = loader.project_mapping["PWD"]
|
||||||
|
debugtalk_functions = loader.project_mapping["functions"]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
project_working_directory,
|
project_working_directory,
|
||||||
os.path.join(os.getcwd(), "tests")
|
os.path.join(os.getcwd(), "tests")
|
||||||
)
|
)
|
||||||
self.assertIn("gen_md5", debugtalk_functions)
|
self.assertIn("gen_md5", debugtalk_functions)
|
||||||
|
|
||||||
project_working_directory, debugtalk_functions = loader.load_debugtalk_py("tests/base.py")
|
loader.load_project_tests("tests/base.py")
|
||||||
|
project_working_directory = loader.project_mapping["PWD"]
|
||||||
|
debugtalk_functions = loader.project_mapping["functions"]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
project_working_directory,
|
project_working_directory,
|
||||||
os.path.join(os.getcwd(), "tests")
|
os.path.join(os.getcwd(), "tests")
|
||||||
)
|
)
|
||||||
self.assertIn("gen_md5", debugtalk_functions)
|
self.assertIn("gen_md5", debugtalk_functions)
|
||||||
|
|
||||||
project_working_directory, debugtalk_functions = loader.load_debugtalk_py("httprunner/__init__.py")
|
loader.load_project_tests("httprunner/__init__.py")
|
||||||
|
project_working_directory = loader.project_mapping["PWD"]
|
||||||
|
debugtalk_functions = loader.project_mapping["functions"]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
project_working_directory,
|
project_working_directory,
|
||||||
os.getcwd()
|
os.getcwd()
|
||||||
|
|||||||
Reference in New Issue
Block a user