fix: handle cases when parent directory name includes dot/hyphen/space

This commit is contained in:
debugtalk
2020-06-05 19:42:01 +08:00
parent 87313a4f89
commit 866cd612f4
28 changed files with 35 additions and 81 deletions
+1
View File
@@ -5,6 +5,7 @@
**Fixed** **Fixed**
- fix: miss formatting referenced testcase - fix: miss formatting referenced testcase
- fix: handle cases when parent directory name includes dot/hyphen/space
**Changed** **Changed**
@@ -5,11 +5,11 @@ config:
testcases: testcases:
- -
name: request with functions name: request with functions
testcase: request_methods/request_with_functions.yml testcase: request.methods/request_with_functions.yml
variables: variables:
var1: testsuite_val1 var1: testsuite_val1
- -
name: request with referenced testcase name: request with referenced testcase
testcase: request_methods/request_with_testcase_reference.yml testcase: request.methods/request_with_testcase_reference.yml
variables: variables:
var2: testsuite_val2 var2: testsuite_val2
@@ -10,7 +10,7 @@ teststeps:
name: request with functions name: request with functions
variables: variables:
foo1: override_bar1 foo1: override_bar1
testcase: request_methods/request_with_functions.yml testcase: request.methods/request_with_functions.yml
export: export:
- session_foo2 - session_foo2
- -
@@ -0,0 +1 @@
# NOTICE: Generated By HttpRunner. DO NOT EDIT!
@@ -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
@@ -1,5 +1,5 @@
# NOTICE: Generated By HttpRunner v3.0.9 # 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 from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
@@ -1,5 +1,5 @@
# NOTICE: Generated By HttpRunner v3.0.9 # 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 os
import sys import sys
@@ -1,5 +1,5 @@
# NOTICE: Generated By HttpRunner v3.0.9 # 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 from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
@@ -1,5 +1,5 @@
# NOTICE: Generated By HttpRunner v3.0.9 # 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 from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
@@ -1,5 +1,5 @@
# NOTICE: Generated By HttpRunner v3.0.9 # 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 os
import sys import sys
@@ -1,5 +1,5 @@
# NOTICE: Generated By HttpRunner v3.0.9 # 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 from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
@@ -1,5 +1,5 @@
# NOTICE: Generated By HttpRunner v3.0.9 # 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 from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
@@ -1,5 +1,5 @@
# NOTICE: Generated By HttpRunner v3.0.9 # 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 from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
+21 -8
View File
@@ -108,6 +108,17 @@ def __ensure_testcase_module(path: Text) -> NoReturn:
f.write("# NOTICE: Generated By HttpRunner. DO NOT EDIT!\n") 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]: def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]:
"""convert single YAML/JSON testcase path to python file""" """convert single YAML/JSON testcase path to python file"""
testcase_path = __ensure_file_name(testcase_path) 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("-", "_") file_name = raw_file_name.replace(" ", "_").replace(".", "_").replace("-", "_")
testcase_dir = os.path.dirname(testcase_path) 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") testcase_python_path = os.path.join(testcase_dir, f"{file_name}_test.py")
# convert title case, e.g. request_with_variables => RequestWithVariables # 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) 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: with open(testcase_python_path, "w", encoding="utf-8") as f:
f.write(content) f.write(content)
@@ -345,7 +354,7 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
load_testsuite(testsuite) load_testsuite(testsuite)
testsuite_config = testsuite["config"] testsuite_config = testsuite["config"]
testsuite_path = testsuite_config["path"] testsuite_path = __ensure_absolute(testsuite_config["path"])
testsuite_variables = testsuite_config.get("variables", {}) testsuite_variables = testsuite_config.get("variables", {})
if isinstance(testsuite_variables, Text): if isinstance(testsuite_variables, Text):
@@ -358,11 +367,15 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
logger.info(f"start to make testsuite: {testsuite_path}") logger.info(f"start to make testsuite: {testsuite_path}")
# create directory with testsuite file name, put its testcases under this directory # create directory with testsuite file name, put its testcases under this directory
testsuite_dir = os.path.join( testsuite_file_name, file_suffix = os.path.splitext(testsuite_path)
os.path.dirname(testsuite_path),
os.path.basename(testsuite_path).replace(".", "_"), 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"
) )
os.makedirs(testsuite_dir, exist_ok=True)
testsuite_dir = __ensure_dir_path_valid(testsuite_file_name)
for testcase in testsuite["testcases"]: for testcase in testsuite["testcases"]:
# get referenced testcase content # get referenced testcase content