diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 8af01f1f..d6f55e08 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,6 +5,7 @@ **Fixed** - fix: miss formatting referenced testcase +- fix: handle cases when parent directory name includes dot/hyphen/space **Changed** diff --git a/examples/postman_echo/__init__.py b/examples/postman-echo/__init__.py similarity index 100% rename from examples/postman_echo/__init__.py rename to examples/postman-echo/__init__.py diff --git a/examples/postman_echo/cookie_manipulation/__init__.py b/examples/postman-echo/cookie_manipulation/__init__.py similarity index 100% rename from examples/postman_echo/cookie_manipulation/__init__.py rename to examples/postman-echo/cookie_manipulation/__init__.py diff --git a/examples/postman_echo/cookie_manipulation/hardcode.yml b/examples/postman-echo/cookie_manipulation/hardcode.yml similarity index 100% rename from examples/postman_echo/cookie_manipulation/hardcode.yml rename to examples/postman-echo/cookie_manipulation/hardcode.yml diff --git a/examples/postman_echo/cookie_manipulation/hardcode_test.py b/examples/postman-echo/cookie_manipulation/hardcode_test.py similarity index 100% rename from examples/postman_echo/cookie_manipulation/hardcode_test.py rename to examples/postman-echo/cookie_manipulation/hardcode_test.py diff --git a/examples/postman_echo/cookie_manipulation/set_delete_cookies.py b/examples/postman-echo/cookie_manipulation/set_delete_cookies.py similarity index 100% rename from examples/postman_echo/cookie_manipulation/set_delete_cookies.py rename to examples/postman-echo/cookie_manipulation/set_delete_cookies.py diff --git a/examples/postman_echo/cookie_manipulation/set_delete_cookies.yml b/examples/postman-echo/cookie_manipulation/set_delete_cookies.yml similarity index 100% rename from examples/postman_echo/cookie_manipulation/set_delete_cookies.yml rename to examples/postman-echo/cookie_manipulation/set_delete_cookies.yml diff --git a/examples/postman_echo/cookie_manipulation/set_delete_cookies_test.py b/examples/postman-echo/cookie_manipulation/set_delete_cookies_test.py similarity index 100% rename from examples/postman_echo/cookie_manipulation/set_delete_cookies_test.py rename to examples/postman-echo/cookie_manipulation/set_delete_cookies_test.py diff --git a/examples/postman_echo/debugtalk.py b/examples/postman-echo/debugtalk.py similarity index 100% rename from examples/postman_echo/debugtalk.py rename to examples/postman-echo/debugtalk.py diff --git a/examples/postman_echo/request_methods/demo_testsuite.yml b/examples/postman-echo/request.methods/demo_testsuite.yml similarity index 67% rename from examples/postman_echo/request_methods/demo_testsuite.yml rename to examples/postman-echo/request.methods/demo_testsuite.yml index 946d37d7..d30490fc 100644 --- a/examples/postman_echo/request_methods/demo_testsuite.yml +++ b/examples/postman-echo/request.methods/demo_testsuite.yml @@ -5,11 +5,11 @@ config: testcases: - name: request with functions - testcase: request_methods/request_with_functions.yml + testcase: request.methods/request_with_functions.yml variables: var1: testsuite_val1 - name: request with referenced testcase - testcase: request_methods/request_with_testcase_reference.yml + testcase: request.methods/request_with_testcase_reference.yml variables: var2: testsuite_val2 diff --git a/examples/postman_echo/request_methods/hardcode.yml b/examples/postman-echo/request.methods/hardcode.yml similarity index 100% rename from examples/postman_echo/request_methods/hardcode.yml rename to examples/postman-echo/request.methods/hardcode.yml diff --git a/examples/postman_echo/request_methods/request_with_functions.yml b/examples/postman-echo/request.methods/request_with_functions.yml similarity index 100% rename from examples/postman_echo/request_methods/request_with_functions.yml rename to examples/postman-echo/request.methods/request_with_functions.yml diff --git a/examples/postman_echo/request_methods/request_with_testcase_reference.yml b/examples/postman-echo/request.methods/request_with_testcase_reference.yml similarity index 92% rename from examples/postman_echo/request_methods/request_with_testcase_reference.yml rename to examples/postman-echo/request.methods/request_with_testcase_reference.yml index 37871f2c..c66dd56d 100644 --- a/examples/postman_echo/request_methods/request_with_testcase_reference.yml +++ b/examples/postman-echo/request.methods/request_with_testcase_reference.yml @@ -10,7 +10,7 @@ teststeps: name: request with functions variables: foo1: override_bar1 - testcase: request_methods/request_with_functions.yml + testcase: request.methods/request_with_functions.yml export: - session_foo2 - diff --git a/examples/postman_echo/request_methods/request_with_variables.yml b/examples/postman-echo/request.methods/request_with_variables.yml similarity index 100% rename from examples/postman_echo/request_methods/request_with_variables.yml rename to examples/postman-echo/request.methods/request_with_variables.yml diff --git a/examples/postman_echo/request_methods/validate_with_functions.yml b/examples/postman-echo/request.methods/validate_with_functions.yml similarity index 100% rename from examples/postman_echo/request_methods/validate_with_functions.yml rename to examples/postman-echo/request.methods/validate_with_functions.yml diff --git a/examples/postman_echo/request_methods/validate_with_variables.yml b/examples/postman-echo/request.methods/validate_with_variables.yml similarity index 100% rename from examples/postman_echo/request_methods/validate_with_variables.yml rename to examples/postman-echo/request.methods/validate_with_variables.yml diff --git a/examples/postman_echo/request_methods/__init__.py b/examples/postman_echo/request_methods/__init__.py index e69de29b..70cfba53 100644 --- a/examples/postman_echo/request_methods/__init__.py +++ b/examples/postman_echo/request_methods/__init__.py @@ -0,0 +1 @@ +# NOTICE: Generated By HttpRunner. DO NOT EDIT! diff --git a/examples/postman_echo/request_methods/conftest.py b/examples/postman_echo/request_methods/conftest.py deleted file mode 100644 index 788c2686..00000000 --- a/examples/postman_echo/request_methods/conftest.py +++ /dev/null @@ -1,61 +0,0 @@ -import uuid -from typing import List - -import pytest -from httprunner import Config, Step -from loguru import logger - - -@pytest.fixture(scope="session", autouse=True) -def session_fixture(request): - """setup and teardown each task""" - total_testcases_num = request.node.testscollected - testcases = [] - for item in request.node.items: - testcase = { - "name": item.cls.config.name, - "path": item.cls.config.path, - "node_id": item.nodeid, - } - testcases.append(testcase) - - logger.debug(f"collected {total_testcases_num} testcases: {testcases}") - - yield - - logger.debug(f"teardown task fixture") - - # teardown task - # TODO: upload task summary - - -@pytest.fixture(scope="function", autouse=True) -def testcase_fixture(request): - """setup and teardown each testcase""" - config: Config = request.cls.config - teststeps: List[Step] = request.cls.teststeps - - logger.debug(f"setup testcase fixture: {config.name} - {request.module.__name__}") - - def update_request_headers(steps, index): - for teststep in steps: - if teststep.request: - index += 1 - teststep.request.headers["X-Request-ID"] = f"{prefix}-{index}" - elif teststep.testcase and hasattr(teststep.testcase, "teststeps"): - update_request_headers(teststep.testcase.teststeps, index) - - # you can update testcase teststep like this - prefix = f"HRUN-{uuid.uuid4()}" - update_request_headers(teststeps, 0) - - yield - - logger.debug( - f"teardown testcase fixture: {config.name} - {request.module.__name__}" - ) - - summary = request.instance.get_summary() - logger.debug(f"testcase result summary: {summary}") - - # TODO: upload testcase summary diff --git a/examples/postman_echo/request_methods/demo_testsuite_yml/__init__.py b/examples/postman_echo/request_methods/demo_testsuite/__init__.py similarity index 100% rename from examples/postman_echo/request_methods/demo_testsuite_yml/__init__.py rename to examples/postman_echo/request_methods/demo_testsuite/__init__.py diff --git a/examples/postman_echo/request_methods/demo_testsuite_yml/request_with_functions_test.py b/examples/postman_echo/request_methods/demo_testsuite/request_with_functions_test.py similarity index 97% rename from examples/postman_echo/request_methods/demo_testsuite_yml/request_with_functions_test.py rename to examples/postman_echo/request_methods/demo_testsuite/request_with_functions_test.py index 474e99c2..f4cb1e6b 100644 --- a/examples/postman_echo/request_methods/demo_testsuite_yml/request_with_functions_test.py +++ b/examples/postman_echo/request_methods/demo_testsuite/request_with_functions_test.py @@ -1,5 +1,5 @@ # NOTICE: Generated By HttpRunner v3.0.9 -# FROM: examples/postman_echo/request_methods/request_with_functions.yml +# FROM: examples/postman-echo/request.methods/request_with_functions.yml from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase diff --git a/examples/postman_echo/request_methods/demo_testsuite_yml/request_with_testcase_reference_test.py b/examples/postman_echo/request_methods/demo_testsuite/request_with_testcase_reference_test.py similarity index 96% rename from examples/postman_echo/request_methods/demo_testsuite_yml/request_with_testcase_reference_test.py rename to examples/postman_echo/request_methods/demo_testsuite/request_with_testcase_reference_test.py index 35a113d2..d968ae68 100644 --- a/examples/postman_echo/request_methods/demo_testsuite_yml/request_with_testcase_reference_test.py +++ b/examples/postman_echo/request_methods/demo_testsuite/request_with_testcase_reference_test.py @@ -1,5 +1,5 @@ # NOTICE: Generated By HttpRunner v3.0.9 -# FROM: examples/postman_echo/request_methods/request_with_testcase_reference.yml +# FROM: examples/postman-echo/request.methods/request_with_testcase_reference.yml import os import sys diff --git a/examples/postman_echo/request_methods/hardcode_test.py b/examples/postman_echo/request_methods/hardcode_test.py index a499659b..d296aac3 100644 --- a/examples/postman_echo/request_methods/hardcode_test.py +++ b/examples/postman_echo/request_methods/hardcode_test.py @@ -1,5 +1,5 @@ # NOTICE: Generated By HttpRunner v3.0.9 -# FROM: examples/postman_echo/request_methods/hardcode.yml +# FROM: examples/postman-echo/request.methods/hardcode.yml from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase diff --git a/examples/postman_echo/request_methods/request_with_functions_test.py b/examples/postman_echo/request_methods/request_with_functions_test.py index 340749f5..2ba75345 100644 --- a/examples/postman_echo/request_methods/request_with_functions_test.py +++ b/examples/postman_echo/request_methods/request_with_functions_test.py @@ -1,5 +1,5 @@ # NOTICE: Generated By HttpRunner v3.0.9 -# FROM: examples/postman_echo/request_methods/request_with_functions.yml +# FROM: examples/postman-echo/request.methods/request_with_functions.yml from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase diff --git a/examples/postman_echo/request_methods/request_with_testcase_reference_test.py b/examples/postman_echo/request_methods/request_with_testcase_reference_test.py index 75c211ff..8c5b4b8e 100644 --- a/examples/postman_echo/request_methods/request_with_testcase_reference_test.py +++ b/examples/postman_echo/request_methods/request_with_testcase_reference_test.py @@ -1,5 +1,5 @@ # NOTICE: Generated By HttpRunner v3.0.9 -# FROM: examples/postman_echo/request_methods/request_with_testcase_reference.yml +# FROM: examples/postman-echo/request.methods/request_with_testcase_reference.yml import os import sys diff --git a/examples/postman_echo/request_methods/request_with_variables_test.py b/examples/postman_echo/request_methods/request_with_variables_test.py index 47eff628..f2f6ca44 100644 --- a/examples/postman_echo/request_methods/request_with_variables_test.py +++ b/examples/postman_echo/request_methods/request_with_variables_test.py @@ -1,5 +1,5 @@ # NOTICE: Generated By HttpRunner v3.0.9 -# FROM: examples/postman_echo/request_methods/request_with_variables.yml +# FROM: examples/postman-echo/request.methods/request_with_variables.yml from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase diff --git a/examples/postman_echo/request_methods/validate_with_functions_test.py b/examples/postman_echo/request_methods/validate_with_functions_test.py index b077ebc8..e2c049fd 100644 --- a/examples/postman_echo/request_methods/validate_with_functions_test.py +++ b/examples/postman_echo/request_methods/validate_with_functions_test.py @@ -1,5 +1,5 @@ # NOTICE: Generated By HttpRunner v3.0.9 -# FROM: examples/postman_echo/request_methods/validate_with_functions.yml +# FROM: examples/postman-echo/request.methods/validate_with_functions.yml from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase diff --git a/examples/postman_echo/request_methods/validate_with_variables_test.py b/examples/postman_echo/request_methods/validate_with_variables_test.py index c333f462..72bd3aff 100644 --- a/examples/postman_echo/request_methods/validate_with_variables_test.py +++ b/examples/postman_echo/request_methods/validate_with_variables_test.py @@ -1,5 +1,5 @@ # NOTICE: Generated By HttpRunner v3.0.9 -# FROM: examples/postman_echo/request_methods/validate_with_variables.yml +# FROM: examples/postman-echo/request.methods/validate_with_variables.yml from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase diff --git a/httprunner/make.py b/httprunner/make.py index 4eb04972..58e50fd8 100644 --- a/httprunner/make.py +++ b/httprunner/make.py @@ -108,6 +108,17 @@ def __ensure_testcase_module(path: Text) -> NoReturn: f.write("# NOTICE: Generated By HttpRunner. DO NOT EDIT!\n") +def __ensure_dir_path_valid(dir_path: Text) -> Text: + # handle cases when parent directory name includes dot/hyphen/space + relative_dir_path = dir_path[len(os.getcwd()) + 1:] + relative_dir_path = ( + relative_dir_path.replace(" ", "_").replace(".", "_").replace("-", "_") + ) + dir_path = os.path.join(os.getcwd(), relative_dir_path) + os.makedirs(dir_path, exist_ok=True) + return dir_path + + def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]: """convert single YAML/JSON testcase path to python file""" testcase_path = __ensure_file_name(testcase_path) @@ -121,6 +132,7 @@ def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]: file_name = raw_file_name.replace(" ", "_").replace(".", "_").replace("-", "_") testcase_dir = os.path.dirname(testcase_path) + testcase_dir = __ensure_dir_path_valid(testcase_dir) testcase_python_path = os.path.join(testcase_dir, f"{file_name}_test.py") # convert title case, e.g. request_with_variables => RequestWithVariables @@ -325,9 +337,6 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text: } content = __TEMPLATE__.render(data) - testcase_python_dir = os.path.dirname(testcase_python_path) - if not os.path.exists(testcase_python_dir): - os.makedirs(testcase_python_dir) with open(testcase_python_path, "w", encoding="utf-8") as f: f.write(content) @@ -345,7 +354,7 @@ def make_testsuite(testsuite: Dict) -> NoReturn: load_testsuite(testsuite) testsuite_config = testsuite["config"] - testsuite_path = testsuite_config["path"] + testsuite_path = __ensure_absolute(testsuite_config["path"]) testsuite_variables = testsuite_config.get("variables", {}) if isinstance(testsuite_variables, Text): @@ -358,11 +367,15 @@ def make_testsuite(testsuite: Dict) -> NoReturn: logger.info(f"start to make testsuite: {testsuite_path}") # create directory with testsuite file name, put its testcases under this directory - testsuite_dir = os.path.join( - os.path.dirname(testsuite_path), - os.path.basename(testsuite_path).replace(".", "_"), - ) - os.makedirs(testsuite_dir, exist_ok=True) + testsuite_file_name, file_suffix = os.path.splitext(testsuite_path) + + file_suffix = file_suffix.lower() + if file_suffix not in [".json", ".yml", ".yaml", ".har"]: + raise exceptions.ParamsError( + "testsuite file should have .yaml/.yml/.json suffix" + ) + + testsuite_dir = __ensure_dir_path_valid(testsuite_file_name) for testcase in testsuite["testcases"]: # get referenced testcase content