mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-13 17:29:56 +08:00
fix: ensure generated conftest.py located in generated pytest folder
This commit is contained in:
@@ -9,7 +9,7 @@ from loguru import logger
|
||||
|
||||
from httprunner import exceptions
|
||||
from httprunner.loader import load_project_meta
|
||||
from httprunner.utils import sort_dict_by_custom_order
|
||||
from httprunner.utils import sort_dict_by_custom_order, ensure_file_path_valid
|
||||
|
||||
|
||||
def convert_jmespath(raw: Text) -> Text:
|
||||
@@ -221,6 +221,7 @@ def generate_conftest_for_summary(args: List):
|
||||
|
||||
project_meta = load_project_meta(test_path)
|
||||
conftest_path = os.path.join(project_meta.RootDir, "conftest.py")
|
||||
conftest_path = ensure_file_path_valid(conftest_path)
|
||||
if os.path.isfile(conftest_path):
|
||||
return
|
||||
|
||||
@@ -308,6 +309,10 @@ def session_fixture(request):
|
||||
"{{SUMMARY_PATH_PLACEHOLDER}}", summary_path
|
||||
)
|
||||
|
||||
dir_path = os.path.dirname(conftest_path)
|
||||
if not os.path.exists(dir_path):
|
||||
os.makedirs(dir_path)
|
||||
|
||||
with open(conftest_path, "w", encoding="utf-8") as f:
|
||||
f.write(conftest_content)
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import os
|
||||
import string
|
||||
import subprocess
|
||||
from shutil import copyfile
|
||||
from typing import Text, List, Tuple, Dict, Set, NoReturn
|
||||
@@ -19,6 +18,7 @@ from httprunner.loader import (
|
||||
)
|
||||
from httprunner.parser import parse_data
|
||||
from httprunner.response import uniform_validator
|
||||
from httprunner.utils import ensure_file_path_valid
|
||||
|
||||
""" cache converted pytest files, avoid duplicate making
|
||||
"""
|
||||
@@ -123,41 +123,6 @@ def __ensure_project_meta_files(tests_path: Text) -> NoReturn:
|
||||
copyfile(dot_csv_path, dot_csv_new_path)
|
||||
|
||||
|
||||
def ensure_file_path_valid(file_path: Text) -> Text:
|
||||
""" ensure file path valid for pytest
|
||||
|
||||
Args:
|
||||
file_path: absolute or relative file path
|
||||
|
||||
Returns:
|
||||
ensured valid absolute file path
|
||||
|
||||
"""
|
||||
raw_file_name, file_suffix = os.path.splitext(file_path)
|
||||
file_suffix = file_suffix.lower()
|
||||
|
||||
if os.path.isabs(file_path):
|
||||
raw_file_relative_name = raw_file_name[len(os.getcwd()) + 1 :]
|
||||
else:
|
||||
raw_file_relative_name = raw_file_name
|
||||
|
||||
path_names = []
|
||||
for name in raw_file_relative_name.split(os.sep):
|
||||
|
||||
if name[0] in string.digits:
|
||||
# ensure file name not startswith digit
|
||||
# 19 => T19, 2C => T2C
|
||||
name = f"T{name}"
|
||||
|
||||
# handle cases when directory name includes dot/hyphen/space
|
||||
name = name.replace(" ", "_").replace(".", "_").replace("-", "_")
|
||||
|
||||
path_names.append(name)
|
||||
|
||||
new_file_path = os.path.join(os.getcwd(), f"{os.sep.join(path_names)}{file_suffix}")
|
||||
return new_file_path
|
||||
|
||||
|
||||
def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]:
|
||||
"""convert single YAML/JSON testcase path to python file"""
|
||||
testcase_new_path = ensure_file_path_valid(testcase_path)
|
||||
|
||||
@@ -2,8 +2,9 @@ import collections
|
||||
import json
|
||||
import os.path
|
||||
import platform
|
||||
import string
|
||||
import uuid
|
||||
from typing import Dict, List, Any
|
||||
from typing import Dict, List, Any, Text
|
||||
|
||||
import sentry_sdk
|
||||
from loguru import logger
|
||||
@@ -176,3 +177,38 @@ def sort_dict_by_custom_order(raw_dict: Dict, custom_order: List):
|
||||
return dict(
|
||||
sorted(raw_dict.items(), key=lambda i: get_index_from_list(custom_order, i[0]))
|
||||
)
|
||||
|
||||
|
||||
def ensure_file_path_valid(file_path: Text) -> Text:
|
||||
""" ensure file path valid for pytest, handle cases when directory name includes dot/hyphen/space
|
||||
|
||||
Args:
|
||||
file_path: absolute or relative file path
|
||||
|
||||
Returns:
|
||||
ensured valid absolute file path
|
||||
|
||||
"""
|
||||
raw_file_name, file_suffix = os.path.splitext(file_path)
|
||||
file_suffix = file_suffix.lower()
|
||||
|
||||
if os.path.isabs(file_path):
|
||||
raw_file_relative_name = raw_file_name[len(os.getcwd()) + 1 :]
|
||||
else:
|
||||
raw_file_relative_name = raw_file_name
|
||||
|
||||
path_names = []
|
||||
for name in raw_file_relative_name.split(os.sep):
|
||||
|
||||
if name[0] in string.digits:
|
||||
# ensure file name not startswith digit
|
||||
# 19 => T19, 2C => T2C
|
||||
name = f"T{name}"
|
||||
|
||||
# handle cases when directory name includes dot/hyphen/space
|
||||
name = name.replace(" ", "_").replace(".", "_").replace("-", "_")
|
||||
|
||||
path_names.append(name)
|
||||
|
||||
new_file_path = os.path.join(os.getcwd(), f"{os.sep.join(path_names)}{file_suffix}")
|
||||
return new_file_path
|
||||
|
||||
@@ -153,9 +153,7 @@ class TestCompat(unittest.TestCase):
|
||||
compat.ensure_cli_args(args2),
|
||||
["examples/postman-echo/request.methods/hardcode.yml"],
|
||||
)
|
||||
self.assertTrue(
|
||||
os.path.isfile("examples/postman_echo/request_methods/conftest.py")
|
||||
)
|
||||
self.assertTrue(os.path.isfile("examples/postman_echo/conftest.py"))
|
||||
|
||||
args3 = [
|
||||
"examples/postman-echo/request.methods/hardcode.yml",
|
||||
|
||||
@@ -8,7 +8,6 @@ from httprunner.make import (
|
||||
make_config_chain_style,
|
||||
make_teststep_chain_style,
|
||||
pytest_files_run_set,
|
||||
ensure_file_path_valid,
|
||||
)
|
||||
|
||||
|
||||
@@ -67,34 +66,6 @@ from examples.postman_echo.request_methods.request_with_functions_test import (
|
||||
testcase_python_list,
|
||||
)
|
||||
|
||||
def test_ensure_file_path_valid(self):
|
||||
self.assertEqual(
|
||||
ensure_file_path_valid(
|
||||
"examples/postman-echo/request.methods/hardcode.yml"
|
||||
),
|
||||
os.path.join(
|
||||
os.getcwd(), "examples/postman_echo/request_methods/hardcode.yml"
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
ensure_file_path_valid(
|
||||
os.path.join(os.getcwd(), "postman-echo/request.methods/hardcode.yml")
|
||||
),
|
||||
os.path.join(os.getcwd(), "postman_echo/request_methods/hardcode.yml"),
|
||||
)
|
||||
self.assertEqual(
|
||||
ensure_file_path_valid(
|
||||
"examples/postman echo/request methods/hardcode.yml"
|
||||
),
|
||||
os.path.join(
|
||||
os.getcwd(), "examples/postman_echo/request_methods/hardcode.yml"
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
ensure_file_path_valid("1/2B/3.yml"),
|
||||
os.path.join(os.getcwd(), "T1/T2B/T3.yml"),
|
||||
)
|
||||
|
||||
def test_convert_testcase_path(self):
|
||||
self.assertEqual(
|
||||
convert_testcase_path("mubu.login.yml"),
|
||||
|
||||
@@ -2,6 +2,7 @@ import os
|
||||
import unittest
|
||||
|
||||
from httprunner import loader, utils
|
||||
from httprunner.utils import ensure_file_path_valid
|
||||
|
||||
|
||||
class TestUtils(unittest.TestCase):
|
||||
@@ -97,3 +98,31 @@ class TestUtils(unittest.TestCase):
|
||||
),
|
||||
["A", "D", "C", "B"],
|
||||
)
|
||||
|
||||
def test_ensure_file_path_valid(self):
|
||||
self.assertEqual(
|
||||
ensure_file_path_valid(
|
||||
"examples/postman-echo/request.methods/hardcode.yml"
|
||||
),
|
||||
os.path.join(
|
||||
os.getcwd(), "examples/postman_echo/request_methods/hardcode.yml"
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
ensure_file_path_valid(
|
||||
os.path.join(os.getcwd(), "postman-echo/request.methods/hardcode.yml")
|
||||
),
|
||||
os.path.join(os.getcwd(), "postman_echo/request_methods/hardcode.yml"),
|
||||
)
|
||||
self.assertEqual(
|
||||
ensure_file_path_valid(
|
||||
"examples/postman echo/request methods/hardcode.yml"
|
||||
),
|
||||
os.path.join(
|
||||
os.getcwd(), "examples/postman_echo/request_methods/hardcode.yml"
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
ensure_file_path_valid("1/2B/3.yml"),
|
||||
os.path.join(os.getcwd(), "T1/T2B/T3.yml"),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user