diff --git a/.gitignore b/.gitignore index 0487b44f..31a72c46 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,3 @@ logs/% .coverage locustfile.py site/ -.env \ No newline at end of file diff --git a/httprunner/api.py b/httprunner/api.py index 14de03ef..c7c8c5c2 100644 --- a/httprunner/api.py +++ b/httprunner/api.py @@ -18,7 +18,6 @@ class HttpRunner(object): resultclass (class): HtmlTestResult or TextTestResult failfast (bool): False/True, stop the test run on the first error or failure. - dot_env_path (str): .env file path. http_client_session (instance): requests.Session(), or locust.client.Session() instance. Attributes: @@ -46,10 +45,6 @@ class HttpRunner(object): """ loader.reset_loader() - # load .env - dot_env_path = self.kwargs.pop("dot_env_path", None) - loader.load_dot_env_file(dot_env_path) - # load api/testcase definition and debugtalk.py module project_folder_path = os.path.join(os.getcwd(), "tests") # TODO: remove tests loader.load_project_tests(project_folder_path) diff --git a/httprunner/cli.py b/httprunner/cli.py index 9e36e8d4..fc15ab5b 100644 --- a/httprunner/cli.py +++ b/httprunner/cli.py @@ -39,9 +39,6 @@ def main_hrun(): parser.add_argument( '--log-file', help="Write logs to specified file path.") - 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.") @@ -78,7 +75,7 @@ def main_hrun(): create_scaffold(project_path) exit(0) - runner = HttpRunner(failfast=args.failfast, dot_env_path=args.dot_env_path).run(args.testset_paths) + runner = HttpRunner(failfast=args.failfast).run(args.testset_paths) if not args.no_html_report: runner.gen_html_report( diff --git a/httprunner/loader.py b/httprunner/loader.py index 141c2601..5681a454 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -22,6 +22,7 @@ project_mapping = { """ testcases_cache_mapping = {} +project_working_directory = os.getcwd() ############################################################################### @@ -156,12 +157,8 @@ def load_folder_files(folder_path, recursive=True): return file_list -def load_dot_env_file(path): - """ load .env file - - Args: - path (str): .env file path. - If path is None, it will find .env file in current working directory. +def load_dot_env_file(): + """ load .env file, .env file should be located in project working directory. Returns: dict: environment variables mapping @@ -173,18 +170,13 @@ def load_dot_env_file(path): } Raises: - exceptions.FileNotFound: If specified env file is not exist. exceptions.FileFormatError: If env file format is invalid. """ - 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 exceptions.FileNotFound("env file not exist: {}".format(path)) + path = os.path.join(project_working_directory, ".env") + if not os.path.isfile(path): + logger.log_debug(".env file not exist in : {}".format(project_working_directory)) + return {} logger.log_info("Loading environment variables from {}".format(path)) env_variables_mapping = {} @@ -910,6 +902,8 @@ def load_project_tests(folder_path): load_builtin_module() load_api_folder(os.path.join(folder_path, "api")) load_test_folder(os.path.join(folder_path, "suite")) + # load .env + load_dot_env_file() def load_testcases(path): diff --git a/tests/data/test.env b/tests/.env similarity index 100% rename from tests/data/test.env rename to tests/.env diff --git a/tests/test_api.py b/tests/test_api.py index 10831744..03a90722 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -339,14 +339,6 @@ class TestHttpRunner(ApiServerUnittest): self.assertIn("in", summary["details"][0]["in_out"]) self.assertIn("out", summary["details"][0]["in_out"]) - def test_loader(self): - hrunner = HttpRunner(dot_env_path="tests/data/test.env") - self.assertEqual(hrunner.project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH") - self.assertIn("debugtalk", hrunner.project_mapping) - self.assertIn("setup_and_reset", hrunner.project_mapping["def-testcase"]) - self.assertIn("get_token", hrunner.project_mapping["def-api"]) - self.assertIn("setup_and_reset", hrunner.project_mapping["def-testcase"]) - def test_load_tests(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testcase.yml') diff --git a/tests/test_loader.py b/tests/test_loader.py index ea980ccc..9e5dd401 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -133,13 +133,18 @@ class TestFileLoader(unittest.TestCase): self.assertEqual([], files) def test_load_dot_env_file(self): - env_variables_mapping = loader.load_dot_env_file("tests/data/test.env") + loader.project_working_directory = os.path.join( + os.getcwd(), "tests", + ) + env_variables_mapping = loader.load_dot_env_file() self.assertIn("PROJECT_KEY", env_variables_mapping) self.assertEqual(env_variables_mapping["UserName"], "debugtalk") def test_load_env_path_not_exist(self): - with self.assertRaises(exceptions.FileNotFound): - loader.load_dot_env_file("not_exist.env") + loader.project_working_directory = os.path.join( + os.getcwd(), "tests", "data", + ) + loader.load_dot_env_file() def test_locate_file(self): with self.assertRaises(exceptions.FileNotFound): @@ -166,7 +171,7 @@ class TestFileLoader(unittest.TestCase): "tests/debugtalk.py" ) self.assertEqual( - loader.locate_file("tests/data/test.env", "debugtalk.py"), + loader.locate_file("tests/data/demo_testcase.yml", "debugtalk.py"), "tests/debugtalk.py" ) @@ -478,10 +483,11 @@ class TestSuiteLoader(unittest.TestCase): ) def test_load_project_tests(self): - project_dir = os.path.join(os.getcwd(), "tests") - loader.load_project_tests(project_dir) - loader.load_debugtalk_module(project_dir) + loader.project_working_directory = os.path.join(os.getcwd(), "tests") + loader.load_project_tests(loader.project_working_directory) + loader.load_debugtalk_module(loader.project_working_directory) project_mapping = loader.project_mapping self.assertEqual(project_mapping["debugtalk"]["variables"]["SECRET_KEY"], "DebugTalk") self.assertIn("get_token", project_mapping["def-api"]) self.assertIn("setup_and_reset", project_mapping["def-testcase"]) + self.assertEqual(project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH")