mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-06 08:19:45 +08:00
refactor: make testcase
This commit is contained in:
@@ -1,13 +1,18 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from typing import Union, Text, List, Tuple
|
from typing import Union, Text, List, Tuple, Dict
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from httprunner import exceptions
|
from httprunner import exceptions
|
||||||
from httprunner.exceptions import TestCaseFormatError
|
from httprunner.exceptions import TestCaseFormatError
|
||||||
from httprunner.loader import load_testcase_file, load_folder_files
|
from httprunner.loader import (
|
||||||
|
load_testcase_file,
|
||||||
|
load_folder_files,
|
||||||
|
load_test_file,
|
||||||
|
load_testcase,
|
||||||
|
)
|
||||||
|
|
||||||
__TMPL__ = """# NOTICE: Generated By HttpRunner. DO'NOT EDIT!
|
__TMPL__ = """# NOTICE: Generated By HttpRunner. DO'NOT EDIT!
|
||||||
# FROM: {{ testcase_path }}
|
# FROM: {{ testcase_path }}
|
||||||
@@ -51,12 +56,9 @@ def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]:
|
|||||||
return testcase_python_path, name_in_title_case
|
return testcase_python_path, name_in_title_case
|
||||||
|
|
||||||
|
|
||||||
def make_testcase(testcase_path: str) -> Union[str, None]:
|
def make_testcase(testcase: Dict) -> Union[str, None]:
|
||||||
|
testcase_path = testcase["config"]["path"]
|
||||||
logger.info(f"start to make testcase: {testcase_path}")
|
logger.info(f"start to make testcase: {testcase_path}")
|
||||||
try:
|
|
||||||
testcase, _ = load_testcase_file(testcase_path)
|
|
||||||
except TestCaseFormatError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
template = jinja2.Template(__TMPL__)
|
template = jinja2.Template(__TMPL__)
|
||||||
|
|
||||||
@@ -90,21 +92,41 @@ def format_with_black(tests_path: Text):
|
|||||||
|
|
||||||
|
|
||||||
def __make(tests_path: Text) -> List:
|
def __make(tests_path: Text) -> List:
|
||||||
testcases = []
|
test_files = []
|
||||||
if os.path.isdir(tests_path):
|
if os.path.isdir(tests_path):
|
||||||
files_list = load_folder_files(tests_path)
|
files_list = load_folder_files(tests_path)
|
||||||
testcases.extend(files_list)
|
test_files.extend(files_list)
|
||||||
elif os.path.isfile(tests_path):
|
elif os.path.isfile(tests_path):
|
||||||
testcases.append(tests_path)
|
test_files.append(tests_path)
|
||||||
else:
|
else:
|
||||||
raise exceptions.TestcaseNotFound(f"Invalid tests path: {tests_path}")
|
raise exceptions.TestcaseNotFound(f"Invalid tests path: {tests_path}")
|
||||||
|
|
||||||
testcase_path_list = []
|
testcase_path_list = []
|
||||||
for testcase_path in testcases:
|
for test_file in test_files:
|
||||||
testcase_path = make_testcase(testcase_path)
|
try:
|
||||||
if not testcase_path:
|
test_content = load_test_file(test_file)
|
||||||
|
test_content.setdefault("config", {})["path"] = test_file
|
||||||
|
except (exceptions.FileNotFound, exceptions.FileFormatError) as ex:
|
||||||
|
logger.warning(ex)
|
||||||
continue
|
continue
|
||||||
testcase_path_list.append(testcase_path)
|
|
||||||
|
if "teststeps" in test_content:
|
||||||
|
# testcase
|
||||||
|
try:
|
||||||
|
# validate testcase format
|
||||||
|
load_testcase(test_content)
|
||||||
|
except exceptions.TestCaseFormatError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
testcase_file = make_testcase(test_content)
|
||||||
|
testcase_path_list.append(testcase_file)
|
||||||
|
elif "testcases" in test_content:
|
||||||
|
# testsuite
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise exceptions.FileFormatError(
|
||||||
|
f"test file is neither testcase nor testsuite: {test_file}"
|
||||||
|
)
|
||||||
|
|
||||||
if not testcase_path_list:
|
if not testcase_path_list:
|
||||||
logger.warning(f"No valid testcase generated on {tests_path}")
|
logger.warning(f"No valid testcase generated on {tests_path}")
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ from httprunner.ext.make import make_testcase, main_make, convert_testcase_path
|
|||||||
|
|
||||||
class TestLoader(unittest.TestCase):
|
class TestLoader(unittest.TestCase):
|
||||||
def test_make_testcase(self):
|
def test_make_testcase(self):
|
||||||
path = "examples/postman_echo/request_methods/request_with_variables.yml"
|
path = ["examples/postman_echo/request_methods/request_with_variables.yml"]
|
||||||
testcase_python_path = make_testcase(path)
|
testcase_python_list = main_make(path)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase_python_path,
|
testcase_python_list[0],
|
||||||
"examples/postman_echo/request_methods/request_with_variables_test.py",
|
"examples/postman_echo/request_methods/request_with_variables_test.py",
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,42 +21,34 @@ class TestLoader(unittest.TestCase):
|
|||||||
|
|
||||||
def test_convert_testcase_path(self):
|
def test_convert_testcase_path(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
convert_testcase_path("mubu.login.yml")[0],
|
convert_testcase_path("mubu.login.yml")[0], "mubu_login_test.py"
|
||||||
"mubu_login_test.py"
|
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
convert_testcase_path("/path/to/mubu.login.yml")[0],
|
convert_testcase_path("/path/to/mubu.login.yml")[0],
|
||||||
"/path/to/mubu_login_test.py"
|
"/path/to/mubu_login_test.py",
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
convert_testcase_path("/path/to 2/mubu.login.yml")[0],
|
convert_testcase_path("/path/to 2/mubu.login.yml")[0],
|
||||||
"/path/to 2/mubu_login_test.py"
|
"/path/to 2/mubu_login_test.py",
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
convert_testcase_path("/path/to 2/mubu.login.yml")[1],
|
convert_testcase_path("/path/to 2/mubu.login.yml")[1], "MubuLogin"
|
||||||
"MubuLogin"
|
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
convert_testcase_path("mubu login.yml")[0],
|
convert_testcase_path("mubu login.yml")[0], "mubu_login_test.py"
|
||||||
"mubu_login_test.py"
|
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
convert_testcase_path("/path/to 2/mubu login.yml")[1],
|
convert_testcase_path("/path/to 2/mubu login.yml")[1], "MubuLogin"
|
||||||
"MubuLogin"
|
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
convert_testcase_path("/path/to 2/mubu-login.yml")[0],
|
convert_testcase_path("/path/to 2/mubu-login.yml")[0],
|
||||||
"/path/to 2/mubu_login_test.py"
|
"/path/to 2/mubu_login_test.py",
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
convert_testcase_path("/path/to 2/mubu-login.yml")[1],
|
convert_testcase_path("/path/to 2/mubu-login.yml")[1], "MubuLogin"
|
||||||
"MubuLogin"
|
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
convert_testcase_path("/path/to 2/幕布login.yml")[0],
|
convert_testcase_path("/path/to 2/幕布login.yml")[0],
|
||||||
"/path/to 2/幕布login_test.py"
|
"/path/to 2/幕布login_test.py",
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
convert_testcase_path("/path/to/幕布login.yml")[1],
|
|
||||||
"幕布Login"
|
|
||||||
)
|
)
|
||||||
|
self.assertEqual(convert_testcase_path("/path/to/幕布login.yml")[1], "幕布Login")
|
||||||
|
|||||||
@@ -74,22 +74,25 @@ def load_test_file(test_file: Text) -> Dict:
|
|||||||
return test_file_content
|
return test_file_content
|
||||||
|
|
||||||
|
|
||||||
def load_testcase_file(testcase_file: Text) -> Tuple[Dict, TestCase]:
|
def load_testcase(testcase: Dict) -> TestCase:
|
||||||
"""load testcase file and validate with pydantic model"""
|
path = testcase["config"]["path"]
|
||||||
testcase_content = load_test_file(testcase_file)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# validate with pydantic TestCase model
|
# validate with pydantic TestCase model
|
||||||
testcase_obj = TestCase.parse_obj(testcase_content)
|
testcase_obj = TestCase.parse_obj(testcase)
|
||||||
except ValidationError as ex:
|
except ValidationError as ex:
|
||||||
err_msg = f"TestCase ValidationError:\nfile: {testcase_file}\nerror: {ex}"
|
err_msg = f"TestCase ValidationError:\nfile: {path}\nerror: {ex}"
|
||||||
logger.error(err_msg)
|
logger.error(err_msg)
|
||||||
raise exceptions.TestCaseFormatError(err_msg)
|
raise exceptions.TestCaseFormatError(err_msg)
|
||||||
|
|
||||||
testcase_content["config"]["path"] = testcase_file
|
return testcase_obj
|
||||||
testcase_obj.config.path = testcase_file
|
|
||||||
|
|
||||||
return testcase_content, testcase_obj
|
|
||||||
|
def load_testcase_file(testcase_file: Text) -> TestCase:
|
||||||
|
"""load testcase file and validate with pydantic model"""
|
||||||
|
testcase_content = load_test_file(testcase_file)
|
||||||
|
testcase_content.setdefault("config", {})["path"] = testcase_file
|
||||||
|
testcase_obj = load_testcase(testcase_content)
|
||||||
|
return testcase_obj
|
||||||
|
|
||||||
|
|
||||||
def load_dot_env_file(dot_env_path: Text) -> Dict:
|
def load_dot_env_file(dot_env_path: Text) -> Dict:
|
||||||
|
|||||||
@@ -7,14 +7,10 @@ from httprunner import exceptions, loader
|
|||||||
class TestLoader(unittest.TestCase):
|
class TestLoader(unittest.TestCase):
|
||||||
def test_load_testcase_file(self):
|
def test_load_testcase_file(self):
|
||||||
path = "examples/postman_echo/request_methods/request_with_variables.yml"
|
path = "examples/postman_echo/request_methods/request_with_variables.yml"
|
||||||
testcase_json, testcase_obj = loader.load_testcase_file(path)
|
testcase_obj = loader.load_testcase_file(path)
|
||||||
self.assertEqual(
|
|
||||||
testcase_json["config"]["name"], "request methods testcase with variables"
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase_obj.config.name, "request methods testcase with variables"
|
testcase_obj.config.name, "request methods testcase with variables"
|
||||||
)
|
)
|
||||||
self.assertEqual(len(testcase_json["teststeps"]), 3)
|
|
||||||
self.assertEqual(len(testcase_obj.teststeps), 3)
|
self.assertEqual(len(testcase_obj.teststeps), 3)
|
||||||
|
|
||||||
def test_load_json_file_file_format_error(self):
|
def test_load_json_file_file_format_error(self):
|
||||||
|
|||||||
@@ -209,7 +209,7 @@ class HttpRunner(object):
|
|||||||
if not os.path.isfile(path):
|
if not os.path.isfile(path):
|
||||||
raise exceptions.ParamsError(f"Invalid testcase path: {path}")
|
raise exceptions.ParamsError(f"Invalid testcase path: {path}")
|
||||||
|
|
||||||
_, testcase_obj = load_testcase_file(path)
|
testcase_obj = load_testcase_file(path)
|
||||||
return self.run(testcase_obj)
|
return self.run(testcase_obj)
|
||||||
|
|
||||||
def get_step_datas(self) -> List[StepData]:
|
def get_step_datas(self) -> List[StepData]:
|
||||||
|
|||||||
Reference in New Issue
Block a user