refactor: make testcase

This commit is contained in:
debugtalk
2020-05-18 15:55:38 +08:00
parent c9039535da
commit bd71a23843
5 changed files with 63 additions and 50 deletions

View File

@@ -1,13 +1,18 @@
import os
import subprocess
from typing import Union, Text, List, Tuple
from typing import Union, Text, List, Tuple, Dict
import jinja2
from loguru import logger
from httprunner import exceptions
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!
# 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
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}")
try:
testcase, _ = load_testcase_file(testcase_path)
except TestCaseFormatError:
return None
template = jinja2.Template(__TMPL__)
@@ -90,21 +92,41 @@ def format_with_black(tests_path: Text):
def __make(tests_path: Text) -> List:
testcases = []
test_files = []
if os.path.isdir(tests_path):
files_list = load_folder_files(tests_path)
testcases.extend(files_list)
test_files.extend(files_list)
elif os.path.isfile(tests_path):
testcases.append(tests_path)
test_files.append(tests_path)
else:
raise exceptions.TestcaseNotFound(f"Invalid tests path: {tests_path}")
testcase_path_list = []
for testcase_path in testcases:
testcase_path = make_testcase(testcase_path)
if not testcase_path:
for test_file in test_files:
try:
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
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:
logger.warning(f"No valid testcase generated on {tests_path}")

View File

@@ -4,10 +4,10 @@ from httprunner.ext.make import make_testcase, main_make, convert_testcase_path
class TestLoader(unittest.TestCase):
def test_make_testcase(self):
path = "examples/postman_echo/request_methods/request_with_variables.yml"
testcase_python_path = make_testcase(path)
path = ["examples/postman_echo/request_methods/request_with_variables.yml"]
testcase_python_list = main_make(path)
self.assertEqual(
testcase_python_path,
testcase_python_list[0],
"examples/postman_echo/request_methods/request_with_variables_test.py",
)
@@ -21,42 +21,34 @@ class TestLoader(unittest.TestCase):
def test_convert_testcase_path(self):
self.assertEqual(
convert_testcase_path("mubu.login.yml")[0],
"mubu_login_test.py"
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"
"/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"
"/path/to 2/mubu_login_test.py",
)
self.assertEqual(
convert_testcase_path("/path/to 2/mubu.login.yml")[1],
"MubuLogin"
convert_testcase_path("/path/to 2/mubu.login.yml")[1], "MubuLogin"
)
self.assertEqual(
convert_testcase_path("mubu login.yml")[0],
"mubu_login_test.py"
convert_testcase_path("mubu login.yml")[0], "mubu_login_test.py"
)
self.assertEqual(
convert_testcase_path("/path/to 2/mubu login.yml")[1],
"MubuLogin"
convert_testcase_path("/path/to 2/mubu login.yml")[1], "MubuLogin"
)
self.assertEqual(
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(
convert_testcase_path("/path/to 2/mubu-login.yml")[1],
"MubuLogin"
convert_testcase_path("/path/to 2/mubu-login.yml")[1], "MubuLogin"
)
self.assertEqual(
convert_testcase_path("/path/to 2/幕布login.yml")[0],
"/path/to 2/幕布login_test.py"
)
self.assertEqual(
convert_testcase_path("/path/to/幕布login.yml")[1],
"幕布Login"
"/path/to 2/幕布login_test.py",
)
self.assertEqual(convert_testcase_path("/path/to/幕布login.yml")[1], "幕布Login")

View File

@@ -74,22 +74,25 @@ def load_test_file(test_file: Text) -> Dict:
return test_file_content
def load_testcase_file(testcase_file: Text) -> Tuple[Dict, TestCase]:
"""load testcase file and validate with pydantic model"""
testcase_content = load_test_file(testcase_file)
def load_testcase(testcase: Dict) -> TestCase:
path = testcase["config"]["path"]
try:
# validate with pydantic TestCase model
testcase_obj = TestCase.parse_obj(testcase_content)
testcase_obj = TestCase.parse_obj(testcase)
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)
raise exceptions.TestCaseFormatError(err_msg)
testcase_content["config"]["path"] = testcase_file
testcase_obj.config.path = testcase_file
return testcase_obj
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:

View File

@@ -7,14 +7,10 @@ from httprunner import exceptions, loader
class TestLoader(unittest.TestCase):
def test_load_testcase_file(self):
path = "examples/postman_echo/request_methods/request_with_variables.yml"
testcase_json, testcase_obj = loader.load_testcase_file(path)
self.assertEqual(
testcase_json["config"]["name"], "request methods testcase with variables"
)
testcase_obj = loader.load_testcase_file(path)
self.assertEqual(
testcase_obj.config.name, "request methods testcase with variables"
)
self.assertEqual(len(testcase_json["teststeps"]), 3)
self.assertEqual(len(testcase_obj.teststeps), 3)
def test_load_json_file_file_format_error(self):

View File

@@ -209,7 +209,7 @@ class HttpRunner(object):
if not os.path.isfile(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)
def get_step_datas(self) -> List[StepData]: