fix: convert_testcase_path

This commit is contained in:
debugtalk
2020-05-17 10:44:46 +08:00
parent 85f02c763f
commit 0133d62a82
4 changed files with 57 additions and 30 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ def main_run(extra_args):
continue continue
elif os.path.isfile(item): elif os.path.isfile(item):
# replace YAML/JSON file path with generated python file # replace YAML/JSON file path with generated python file
extra_args[index] = convert_testcase_path(item) extra_args[index], _ = convert_testcase_path(item)
tests_path_list.append(item) tests_path_list.append(item)
+3 -3
View File
@@ -22,9 +22,9 @@ class TestHar(TestUtils):
for validator in teststep_dict["validate"] for validator in teststep_dict["validate"]
} }
self.assertEqual(validators_mapping["status_code"], 200) self.assertEqual(validators_mapping["status_code"], 200)
self.assertEqual(validators_mapping["content.IsSuccess"], True) self.assertEqual(validators_mapping["body.IsSuccess"], True)
self.assertEqual(validators_mapping["content.Code"], 200) self.assertEqual(validators_mapping["body.Code"], 200)
self.assertEqual(validators_mapping["content.Message"], None) self.assertEqual(validators_mapping["body.Message"], None)
def test_prepare_teststeps(self): def test_prepare_teststeps(self):
teststeps = self.har_parser._prepare_teststeps() teststeps = self.har_parser._prepare_teststeps()
+26 -25
View File
@@ -1,6 +1,6 @@
import os import os
import subprocess import subprocess
from typing import Union, Text, List from typing import Union, Text, List, Tuple
import jinja2 import jinja2
from loguru import logger from loguru import logger
@@ -28,6 +28,28 @@ if __name__ == "__main__":
""" """
def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]:
"""convert single YAML/JSON testcase path to python file"""
if os.path.isdir(testcase_path):
# folder does not need to convert
return testcase_path, ""
raw_file_name, file_suffix = os.path.splitext(os.path.basename(testcase_path))
file_suffix = file_suffix.lower()
if file_suffix not in [".json", ".yml", ".yaml"]:
raise exceptions.ParamsError("")
file_name = raw_file_name.replace(" ", "_").replace(".", "_")
testcase_dir = os.path.dirname(testcase_path)
testcase_python_path = os.path.join(testcase_dir, f"{file_name}_test.py")
# convert title case, e.g. request_with_variables => RequestWithVariables
name_in_title_case = file_name.title().replace("_", "")
return testcase_python_path, name_in_title_case
def make_testcase(testcase_path: str) -> Union[str, None]: def make_testcase(testcase_path: str) -> Union[str, None]:
logger.info(f"start to make testcase: {testcase_path}") logger.info(f"start to make testcase: {testcase_path}")
try: try:
@@ -37,12 +59,7 @@ def make_testcase(testcase_path: str) -> Union[str, None]:
template = jinja2.Template(__TMPL__) template = jinja2.Template(__TMPL__)
raw_file_name, _ = os.path.splitext(os.path.basename(testcase_path)) testcase_python_path, name_in_title_case = convert_testcase_path(testcase_path)
# convert title case, e.g. request_with_variables => RequestWithVariables
name_in_title_case = raw_file_name.replace(".", " ").title().replace("_", "").replace(" ", "")
testcase_dir = os.path.dirname(testcase_path)
testcase_python_path = os.path.join(testcase_dir, f"{raw_file_name}_test.py")
config = testcase["config"] config = testcase["config"]
config["path"] = testcase_python_path config["path"] = testcase_python_path
@@ -60,26 +77,10 @@ def make_testcase(testcase_path: str) -> Union[str, None]:
return testcase_python_path return testcase_python_path
def convert_testcase_path(testcase_path: Text) -> Text:
"""convert single YAML/JSON testcase path to python file"""
if os.path.isdir(testcase_path):
# folder does not need to convert
return testcase_path
file_suffix = os.path.splitext(testcase_path)[1].lower()
if file_suffix == ".json":
return testcase_path.replace(".json", "_test.py")
elif file_suffix == ".yaml":
return testcase_path.replace(".yaml", "_test.py")
elif file_suffix == ".yml":
return testcase_path.replace(".yml", "_test.py")
else:
raise exceptions.ParamsError("")
def format_with_black(tests_path: Text): def format_with_black(tests_path: Text):
logger.info("format testcases with black ...") logger.info("format testcases with black ...")
tests_path = convert_testcase_path(tests_path) tests_path, _ = convert_testcase_path(tests_path)
try: try:
subprocess.run(["black", tests_path]) subprocess.run(["black", tests_path])
except subprocess.CalledProcessError as ex: except subprocess.CalledProcessError as ex:
+27 -1
View File
@@ -1,5 +1,5 @@
import unittest import unittest
from httprunner.ext.make import make_testcase, main_make from httprunner.ext.make import make_testcase, main_make, convert_testcase_path
class TestLoader(unittest.TestCase): class TestLoader(unittest.TestCase):
@@ -18,3 +18,29 @@ class TestLoader(unittest.TestCase):
"examples/postman_echo/request_methods/request_with_functions_test.py", "examples/postman_echo/request_methods/request_with_functions_test.py",
testcase_python_list, testcase_python_list,
) )
def test_convert_testcase_path(self):
self.assertEqual(
convert_testcase_path("mubu.login.yml")[0],
"mubu_login_test.py"
)
self.assertEqual(
convert_testcase_path("mubu login.yml")[0],
"mubu_login_test.py"
)
self.assertEqual(
convert_testcase_path("/path/to/mubu.login.yml")[0],
"/path/to/mubu_login_test.py"
)
self.assertEqual(
convert_testcase_path("/path/to 2/mubu.login.yml")[0],
"/path/to 2/mubu_login_test.py"
)
self.assertEqual(
convert_testcase_path("/path/to 2/mubu.login.yml")[1],
"MubuLogin"
)
self.assertEqual(
convert_testcase_path("/path/to 2/mubu login.yml")[1],
"MubuLogin"
)