From c0c170a35556eeb44530e1357b74aaf678dae7ed Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 23 Aug 2018 08:33:53 +0800 Subject: [PATCH 1/4] refactor create_scaffold: create new project structure --- httprunner/utils.py | 11 ++++++----- tests/test_utils.py | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/httprunner/utils.py b/httprunner/utils.py index c4a76220..be0c6b75 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -374,11 +374,12 @@ def create_scaffold(project_path): path_list = [ (project_path, "folder"), - (os.path.join(project_path, "tests"), "folder"), - (os.path.join(project_path, "tests", "api"), "folder"), - (os.path.join(project_path, "tests", "suite"), "folder"), - (os.path.join(project_path, "tests", "testcases"), "folder"), - (os.path.join(project_path, "tests", "debugtalk.py"), "file") + (os.path.join(project_path, "api"), "folder"), + (os.path.join(project_path, "testcases"), "folder"), + (os.path.join(project_path, "testsuites"), "folder"), + (os.path.join(project_path, "reports"), "folder"), + (os.path.join(project_path, "debugtalk.py"), "file"), + (os.path.join(project_path, ".env"), "file") ] msg = "" diff --git a/tests/test_utils.py b/tests/test_utils.py index 2a5c699d..5d6fbad8 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -254,11 +254,12 @@ class TestUtils(ApiServerUnittest): def test_create_scaffold(self): project_path = os.path.join(os.getcwd(), "projectABC") utils.create_scaffold(project_path) - self.assertTrue(os.path.isdir(os.path.join(project_path, "tests"))) - self.assertTrue(os.path.isdir(os.path.join(project_path, "tests", "api"))) - self.assertTrue(os.path.isdir(os.path.join(project_path, "tests", "suite"))) - self.assertTrue(os.path.isdir(os.path.join(project_path, "tests", "testcases"))) - self.assertTrue(os.path.isfile(os.path.join(project_path, "tests", "debugtalk.py"))) + self.assertTrue(os.path.isdir(os.path.join(project_path, "api"))) + self.assertTrue(os.path.isdir(os.path.join(project_path, "testcases"))) + self.assertTrue(os.path.isdir(os.path.join(project_path, "testsuites"))) + self.assertTrue(os.path.isdir(os.path.join(project_path, "reports"))) + self.assertTrue(os.path.isfile(os.path.join(project_path, "debugtalk.py"))) + self.assertTrue(os.path.isfile(os.path.join(project_path, ".env"))) shutil.rmtree(project_path) def test_cartesian_product_one(self): From 0648ee9bf63495fb8206c51c9ad2f28c8f6506cc Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 23 Aug 2018 11:52:39 +0800 Subject: [PATCH 2/4] remove --dot-env-path argument --- .gitignore | 1 - httprunner/api.py | 5 ----- httprunner/cli.py | 5 +---- httprunner/loader.py | 24 +++++++++--------------- tests/{data/test.env => .env} | 0 tests/test_api.py | 8 -------- tests/test_loader.py | 20 +++++++++++++------- 7 files changed, 23 insertions(+), 40 deletions(-) rename tests/{data/test.env => .env} (100%) 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") From e2ba773e78a095133d4b03afa87d82e051692634 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 23 Aug 2018 13:03:42 +0800 Subject: [PATCH 3/4] fix #343: locate debugtalk.py and use as project working directory --- httprunner/api.py | 29 ++++++------------- httprunner/loader.py | 65 ++++++++++++++++++++++++++----------------- httprunner/report.py | 4 +-- tests/test_api.py | 6 ++-- tests/test_context.py | 4 +-- tests/test_loader.py | 23 +++++++++------ tests/test_parser.py | 3 +- tests/test_runner.py | 4 +-- 8 files changed, 70 insertions(+), 68 deletions(-) diff --git a/httprunner/api.py b/httprunner/api.py index c7c8c5c2..76d2eebc 100644 --- a/httprunner/api.py +++ b/httprunner/api.py @@ -36,22 +36,6 @@ class HttpRunner(object): self.kwargs = kwargs self.http_client_session = self.kwargs.pop("http_client_session", None) - self.__loader() - - def __loader(self): - """ load project dependent files, including api/testcase definitions, - environment variables and builtin module. - - """ - loader.reset_loader() - - # 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) - - self.project_mapping = loader.project_mapping - utils.set_os_environ(self.project_mapping["env"]) - def load_tests(self, path_or_testcases): """ load testcases, extend and merge with api/testcase definitions. @@ -94,14 +78,14 @@ class HttpRunner(object): if isinstance(path_or_testcases, list): for testcase in path_or_testcases: try: - dir_path = os.path.dirname(testcase["config"]["path"]) - loader.load_debugtalk_module(dir_path) + test_path = os.path.dirname(testcase["config"]["path"]) + loader.load_project_tests(test_path) except KeyError: pass else: try: - dir_path = os.path.dirname(path_or_testcases["config"]["path"]) - loader.load_debugtalk_module(dir_path) + test_path = os.path.dirname(path_or_testcases["config"]["path"]) + loader.load_project_tests(test_path) except KeyError: pass @@ -109,6 +93,8 @@ class HttpRunner(object): else: testcases = loader.load_testcases(path_or_testcases) + self.project_mapping = loader.project_mapping + if not testcases: raise exceptions.TestcaseNotFound @@ -245,8 +231,9 @@ class HttpRunner(object): instance: HttpRunner() instance """ - # parser + # loader testcases_list = self.load_tests(path_or_testcases) + # parser parsed_testcases_list = self.parse_tests(testcases_list) # initialize diff --git a/httprunner/loader.py b/httprunner/loader.py index 5681a454..a073d05c 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -6,7 +6,7 @@ import json import os import yaml -from httprunner import built_in, exceptions, logger, parser, validator +from httprunner import built_in, exceptions, logger, parser, utils, validator from httprunner.compat import OrderedDict project_mapping = { @@ -192,6 +192,8 @@ def load_dot_env_file(): env_variables_mapping[variable.strip()] = value.strip() project_mapping["env"] = env_variables_mapping + utils.set_os_environ(env_variables_mapping) + return env_variables_mapping @@ -231,6 +233,23 @@ def locate_file(start_path, file_name): return locate_file(os.path.dirname(start_dir_path), file_name) +def locate_pwd(start_path): + """ locate project working directory. + The folder contains debugtalk.py will be used as PWD. + If debugtalk.py is not found, use os.getcwd() as default PWD. + + Args: + start_path (str): start locating path, maybe testcase file path or directory path + + """ + global project_working_directory + try: + debugtalk_path = locate_file(start_path, "debugtalk.py") + project_working_directory = os.path.dirname(debugtalk_path) + except exceptions.FileNotFound: + project_working_directory = os.getcwd() + + ############################################################################### ## debugtalk.py module loader ############################################################################### @@ -297,25 +316,18 @@ def load_builtin_module(): project_mapping["debugtalk"] = built_in_module -def load_debugtalk_module(start_path=None): +def load_debugtalk_module(): """ load project debugtalk.py module and merge with builtin module. - - Args: - start_path (str, optional): start locating path, maybe file path or directory path. - Defaults to current working directory. - - Returns: - dict: variables and functions mapping for debugtalk.py + debugtalk.py should be located in project working directory. + variables and functions mapping for debugtalk.py { "variables": {}, "functions": {} } """ - start_path = start_path or os.getcwd() - try: - module_path = locate_file(start_path, "debugtalk.py") + module_path = locate_file(project_working_directory, "debugtalk.py") module_name = convert_module_name(module_path) except exceptions.FileNotFound: return @@ -734,7 +746,7 @@ def load_folder_content(folder_path): return items_mapping -def load_api_folder(api_folder_path=None): +def load_api_folder(api_folder_path): """ load api definitions from api folder. Args: @@ -775,7 +787,6 @@ def load_api_folder(api_folder_path=None): """ api_definition_mapping = {} - api_folder_path = api_folder_path or os.path.join(os.getcwd(), "api") api_items_mapping = load_folder_content(api_folder_path) for api_file_path, api_items in api_items_mapping.items(): @@ -797,7 +808,7 @@ def load_api_folder(api_folder_path=None): return api_definition_mapping -def load_test_folder(test_folder_path=None): +def load_test_folder(test_folder_path): """ load testcases definitions from folder. Args: @@ -839,8 +850,6 @@ def load_test_folder(test_folder_path=None): """ test_definition_mapping = {} - # TODO: replace suite with testcases - test_folder_path = test_folder_path or os.path.join(os.getcwd(), "suite") test_items_mapping = load_folder_content(test_folder_path) for test_file_path, items in test_items_mapping.items(): @@ -882,6 +891,9 @@ def load_test_folder(test_folder_path=None): def reset_loader(): """ reset project mapping. """ + global project_working_directory + project_working_directory = os.getcwd() + project_mapping["debugtalk"] = { "variables": {}, "functions": {} @@ -892,18 +904,22 @@ def reset_loader(): testcases_cache_mapping.clear() -def load_project_tests(folder_path): - """ load api, testcases and builtin module. +def load_project_tests(test_path): + """ load api, testcases, .env, builtin module and debugtalk.py. + api/testcases folder is relative to project_working_directory Args: - folder_path (str): folder path. + test_path (str): test file/folder path, locate pwd from this path. """ + reset_loader() + locate_pwd(test_path) load_builtin_module() - load_api_folder(os.path.join(folder_path, "api")) - load_test_folder(os.path.join(folder_path, "suite")) + load_api_folder(os.path.join(project_working_directory, "api")) + load_test_folder(os.path.join(project_working_directory, "suite")) # load .env load_dot_env_file() + load_debugtalk_module() def load_testcases(path): @@ -942,14 +958,13 @@ def load_testcases(path): return testcases_cache_mapping[path] if os.path.isdir(path): - load_debugtalk_module(path) + load_project_tests(path) files_list = load_folder_files(path) testcases_list = load_testcases(files_list) elif os.path.isfile(path): try: - dir_path = os.path.dirname(path) - load_debugtalk_module(dir_path) + load_project_tests(path) testcase = _load_test_file(path) if testcase["teststeps"]: testcases_list = [testcase] diff --git a/httprunner/report.py b/httprunner/report.py index 531369e5..607a8422 100644 --- a/httprunner/report.py +++ b/httprunner/report.py @@ -9,7 +9,7 @@ from base64 import b64encode from collections import Iterable from datetime import datetime -from httprunner import logger +from httprunner import loader, logger from httprunner.__about__ import __version__ from httprunner.compat import basestring, bytes, json, numeric_types from jinja2 import Template, escape @@ -95,7 +95,7 @@ def render_html_report(summary, html_report_name=None, html_report_template=None logger.log_info("Start to render Html report ...") logger.log_debug("render data: {}".format(summary)) - report_dir_path = os.path.join(os.getcwd(), "reports") + report_dir_path = os.path.join(loader.project_working_directory, "reports") start_at_timestamp = int(summary["time"]["start_at"]) summary["time"]["start_datetime"] = datetime.fromtimestamp(start_at_timestamp).strftime('%Y-%m-%d %H:%M:%S') if html_report_name: diff --git a/tests/test_api.py b/tests/test_api.py index 03a90722..ddd377dd 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -2,7 +2,7 @@ import os import shutil import time -from httprunner import HttpRunner, LocustRunner +from httprunner import HttpRunner, LocustRunner, loader from locust import HttpLocust from tests.api_server import HTTPBIN_SERVER from tests.base import ApiServerUnittest @@ -89,7 +89,7 @@ class TestHttpRunner(ApiServerUnittest): self.assertEqual(summary["stat"]["skipped"], 4) runner.gen_html_report(html_report_name=output_folder_name) - report_save_dir = os.path.join(os.getcwd(), 'reports', output_folder_name) + report_save_dir = os.path.join(loader.project_working_directory, 'reports', output_folder_name) self.assertGreater(len(os.listdir(report_save_dir)), 0) shutil.rmtree(report_save_dir) @@ -155,7 +155,7 @@ class TestHttpRunner(ApiServerUnittest): output_folder_name = os.path.basename(os.path.splitext(testset_path)[0]) report = runner.gen_html_report(html_report_name=output_folder_name) self.assertTrue(os.path.isfile(report)) - report_save_dir = os.path.join(os.getcwd(), 'reports', output_folder_name) + report_save_dir = os.path.join(loader.project_working_directory, 'reports', output_folder_name) shutil.rmtree(report_save_dir) def test_testcase_layer(self): diff --git a/tests/test_context.py b/tests/test_context.py index f1ce57e5..32da0add 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -9,9 +9,7 @@ from tests.base import ApiServerUnittest class TestContext(ApiServerUnittest): def setUp(self): - project_dir = os.path.join(os.getcwd(), "tests") - loader.load_project_tests(project_dir) - loader.load_debugtalk_module(project_dir) + loader.load_project_tests(os.path.join(os.getcwd(), "tests")) self.debugtalk_module = loader.project_mapping["debugtalk"] self.context = context.Context( diff --git a/tests/test_loader.py b/tests/test_loader.py index 9e5dd401..64430354 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -185,15 +185,13 @@ class TestModuleLoader(unittest.TestCase): self.assertNotIn("is_py3", functions_dict) def test_load_debugtalk_module(self): - project_dir = os.path.join(os.getcwd(), "tests") - loader.load_project_tests(project_dir) - loader.load_debugtalk_module() + loader.load_project_tests(os.path.join(os.getcwd(), "httprunner")) imported_module_items = loader.project_mapping["debugtalk"] self.assertIn("equals", imported_module_items["functions"]) self.assertNotIn("SECRET_KEY", imported_module_items["variables"]) self.assertNotIn("alter_response", imported_module_items["functions"]) - loader.load_debugtalk_module("tests") + loader.load_project_tests(os.path.join(os.getcwd(), "tests")) imported_module_items = loader.project_mapping["debugtalk"] self.assertEqual( imported_module_items["variables"]["SECRET_KEY"], @@ -228,13 +226,22 @@ class TestModuleLoader(unittest.TestCase): with self.assertRaises(exceptions.VariableNotFound): loader.get_module_item(module_mapping, "variables", "SECRET_KEY2") + def test_locate_pwd(self): + loader.locate_pwd("tests/data/demo_testcase.yml") + self.assertEqual(loader.project_working_directory, "tests") + + loader.locate_pwd("tests/base.py") + self.assertEqual(loader.project_working_directory, "tests") + + loader.locate_pwd("httprunner/__init__.py") + self.assertEqual(loader.project_working_directory, os.getcwd()) + class TestSuiteLoader(unittest.TestCase): @classmethod def setUpClass(cls): - project_dir = os.path.join(os.getcwd(), "tests") - loader.load_project_tests(project_dir) + loader.load_project_tests(os.path.join(os.getcwd(), "tests")) def test_load_test_file_testcase(self): testcase = loader._load_test_file("tests/testcases/smoketest.yml") @@ -483,9 +490,7 @@ class TestSuiteLoader(unittest.TestCase): ) def test_load_project_tests(self): - 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) + loader.load_project_tests(os.path.join(os.getcwd(), "tests")) project_mapping = loader.project_mapping self.assertEqual(project_mapping["debugtalk"]["variables"]["SECRET_KEY"], "DebugTalk") self.assertIn("get_token", project_mapping["def-api"]) diff --git a/tests/test_parser.py b/tests/test_parser.py index 42c7ebfa..a5f0de0a 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -412,8 +412,7 @@ class TestParser(unittest.TestCase): ) def test_parse_parameters_mix(self): - project_dir = os.path.join(os.getcwd(), "tests") - loader.load_debugtalk_module(project_dir) + loader.load_project_tests(os.path.join(os.getcwd(), "tests")) project_mapping = loader.project_mapping parameters = [ diff --git a/tests/test_runner.py b/tests/test_runner.py index 1e19c5a0..bc55e979 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -10,9 +10,7 @@ from tests.base import ApiServerUnittest class TestRunner(ApiServerUnittest): def setUp(self): - project_dir = os.path.join(os.getcwd(), "tests") - loader.load_project_tests(project_dir) - loader.load_debugtalk_module(project_dir) + loader.load_project_tests(os.path.join(os.getcwd(), "tests")) self.debugtalk_module = loader.project_mapping["debugtalk"] config_dict = { "variables": self.debugtalk_module["variables"], From cb44fafebf58d93ae8fa3ac823eff03ee52de9bc Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 23 Aug 2018 16:11:26 +0800 Subject: [PATCH 4/4] fix #189: remove file path dependency --- httprunner/api.py | 10 +++++----- httprunner/loader.py | 16 ++++++---------- httprunner/runner.py | 1 - httprunner/validator.py | 1 - tests/test_api.py | 9 +++------ tests/test_loader.py | 5 ----- tests/test_runner.py | 14 +++----------- 7 files changed, 17 insertions(+), 39 deletions(-) diff --git a/httprunner/api.py b/httprunner/api.py index 76d2eebc..2e87c5fe 100644 --- a/httprunner/api.py +++ b/httprunner/api.py @@ -52,7 +52,7 @@ class HttpRunner(object): { "config": { "name": "desc1", - "path": "", + "path": "", # optional "variables": [], # optional "request": {} # optional }, @@ -79,15 +79,15 @@ class HttpRunner(object): for testcase in path_or_testcases: try: test_path = os.path.dirname(testcase["config"]["path"]) - loader.load_project_tests(test_path) except KeyError: - pass + test_path = os.getcwd() + loader.load_project_tests(test_path) else: try: test_path = os.path.dirname(path_or_testcases["config"]["path"]) - loader.load_project_tests(test_path) except KeyError: - pass + test_path = os.getcwd() + loader.load_project_tests(test_path) testcases = path_or_testcases else: diff --git a/httprunner/loader.py b/httprunner/loader.py index a073d05c..5aa1de9f 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -424,9 +424,7 @@ def _load_test_file(file_path): """ testcase = { - "config": { - "path": file_path - }, + "config": {}, "teststeps": [] } @@ -856,9 +854,7 @@ def load_test_folder(test_folder_path): # TODO: add JSON schema validation testcase = { - "config": { - "path": test_file_path - }, + "config": {}, "teststeps": [] } for item in items: @@ -914,12 +910,12 @@ def load_project_tests(test_path): """ reset_loader() locate_pwd(test_path) - load_builtin_module() - load_api_folder(os.path.join(project_working_directory, "api")) - load_test_folder(os.path.join(project_working_directory, "suite")) - # load .env load_dot_env_file() + load_builtin_module() load_debugtalk_module() + load_api_folder(os.path.join(project_working_directory, "api")) + # TODO: replace suite with testcases + load_test_folder(os.path.join(project_working_directory, "suite")) def load_testcases(path): diff --git a/httprunner/runner.py b/httprunner/runner.py index 7394cb0b..7342a89a 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -45,7 +45,6 @@ class Runner(object): testcase: { "name": "testcase description", - "path": "tests/data/demo_testset_variables.yml", "variables": [], # optional "request": { "base_url": "http://127.0.0.1:5000", diff --git a/httprunner/validator.py b/httprunner/validator.py index 681a816c..739341df 100644 --- a/httprunner/validator.py +++ b/httprunner/validator.py @@ -14,7 +14,6 @@ def is_testcase(data_structure): { "config": { "name": "desc1", - "path": "", "variables": [], # optional "request": {} # optional }, diff --git a/tests/test_api.py b/tests/test_api.py index ddd377dd..c127f5d2 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -21,7 +21,6 @@ class TestHttpRunner(ApiServerUnittest): self.testcase = { 'name': 'testset description', 'config': { - 'path': 'docs/data/demo-quickstart-2.yml', 'name': 'testset description', 'request': { 'base_url': '', @@ -181,7 +180,7 @@ class TestHttpRunner(ApiServerUnittest): { "config": { "name": "test teardown hooks", - 'path': 'tests/httpbin/hooks.yml', + "path": "tests/httpbin/hooks.yml" }, "teststeps": [ { @@ -217,7 +216,7 @@ class TestHttpRunner(ApiServerUnittest): { "name": "test teardown hooks", "config": { - 'path': 'tests/httpbin/hooks.yml', + "path": "tests/httpbin/hooks.yml" }, "teststeps": [ { @@ -246,9 +245,7 @@ class TestHttpRunner(ApiServerUnittest): testcases = [ { "name": "test teardown hooks", - "config": { - 'path': 'tests/httpbin/hooks.yml', - }, + "config": {}, "teststeps": [ { "name": "test teardown hooks", diff --git a/tests/test_loader.py b/tests/test_loader.py index 64430354..699e8064 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -246,7 +246,6 @@ class TestSuiteLoader(unittest.TestCase): def test_load_test_file_testcase(self): testcase = loader._load_test_file("tests/testcases/smoketest.yml") self.assertEqual(testcase["config"]["name"], "smoketest") - self.assertEqual(testcase["config"]["path"], "tests/testcases/smoketest.yml") self.assertIn("device_sn", testcase["config"]["variables"][0]) self.assertEqual(len(testcase["teststeps"]), 8) self.assertEqual(testcase["teststeps"][0]["name"], "get token") @@ -365,8 +364,6 @@ class TestSuiteLoader(unittest.TestCase): os.getcwd(), 'tests/data/demo_testset_hardcode.json') testset_list = loader.load_testcases(path) self.assertEqual(len(testset_list), 1) - self.assertIn("path", testset_list[0]["config"]) - self.assertEqual(testset_list[0]["config"]["path"], path) self.assertEqual(len(testset_list[0]["teststeps"]), 3) testsets_list.extend(testset_list) @@ -374,8 +371,6 @@ class TestSuiteLoader(unittest.TestCase): path = 'tests/data/demo_testset_hardcode.yml' testset_list = loader.load_testcases(path) self.assertEqual(len(testset_list), 1) - self.assertIn("path", testset_list[0]["config"]) - self.assertIn(path, testset_list[0]["config"]["path"]) self.assertEqual(len(testset_list[0]["teststeps"]), 3) testsets_list.extend(testset_list) diff --git a/tests/test_runner.py b/tests/test_runner.py index bc55e979..5a5ff67f 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -80,7 +80,6 @@ class TestRunner(ApiServerUnittest): start_time = time.time() config_dict = { - "path": os.path.join(os.getcwd(), __file__), "name": "basic test with httpbin", "variables": self.debugtalk_module["variables"], "functions": self.debugtalk_module["functions"], @@ -130,7 +129,6 @@ class TestRunner(ApiServerUnittest): def test_run_testset_with_hooks_modify_request(self): config_dict = { - "path": os.path.join(os.getcwd(), __file__), "name": "basic test with httpbin", "variables": self.debugtalk_module["variables"], "functions": self.debugtalk_module["functions"], @@ -185,9 +183,7 @@ class TestRunner(ApiServerUnittest): ], "teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"] } - config_dict = { - "path": os.path.join(os.getcwd(), __file__) - } + config_dict = {} self.test_runner.init_config(config_dict, "testcase") start_time = time.time() @@ -218,9 +214,7 @@ class TestRunner(ApiServerUnittest): ], "teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"] } - config_dict = { - "path": os.path.join(os.getcwd(), __file__) - } + config_dict = {} self.test_runner.init_config(config_dict, "testcase") start_time = time.time() @@ -246,9 +240,7 @@ class TestRunner(ApiServerUnittest): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/test_bugfix.yml') testcases = loader.load_file(testcase_file_path) - config_dict = { - "path": testcase_file_path - } + config_dict = {} self.test_runner.init_config(config_dict, "testcase") test = testcases[2]["test"]