refactor: ensure_file_path_valid

This commit is contained in:
debugtalk
2020-06-06 00:32:38 +08:00
parent 2a9a05af9b
commit e8275ab38a
13 changed files with 168 additions and 256 deletions

View File

@@ -1,54 +0,0 @@
# NOTICE: Generated By HttpRunner. DO NOT EDIT!
# FROM: examples/postman_echo/cookie_manipulation/hardcode.yml
from httprunner import HttpRunner, TConfig, TStep
class TestCaseHardcode(HttpRunner):
config = TConfig(
**{
"name": "set & delete cookies.",
"base_url": "https://postman-echo.com",
"verify": False,
"export": ["cookie_foo1", "cookie_foo3"],
"path": "examples/postman_echo/cookie_manipulation/hardcode_test.py",
}
)
teststeps = [
TStep(
**{
"name": "set cookie foo1 & foo2 & foo3",
"request": {
"method": "GET",
"url": "/cookies/set",
"params": {"foo1": "bar1", "foo2": "bar2"},
"headers": {"User-Agent": "HttpRunner/${get_httprunner_version()}"},
},
"extract": {"cookie_foo1": "$.cookies.foo1"},
"validate": [
{"eq": ["status_code", 200]},
{"eq": ["cookies.foo1", "bar1"]},
],
}
),
TStep(
**{
"name": "delete cookie foo2",
"request": {
"method": "GET",
"url": "/cookies/delete?foo2",
"headers": {"User-Agent": "HttpRunner/${get_httprunner_version()}"},
},
"validate": [
{"eq": ["status_code", 200]},
{"ne": ["$.cookies.foo1", "$foo1"]},
{"eq": ["$.cookies.foo1", "$cookie_foo1"]},
{"eq": ["$.cookies.foo3", "$cookie_foo3"]},
],
}
),
]
if __name__ == "__main__":
TestCaseHardcode().test_start()

View File

@@ -1,62 +0,0 @@
import unittest
import requests
from httprunner.runner import HttpRunner
from httprunner.models import TConfig, TStep
class TestCaseSetDeleteCookies(unittest.TestCase):
config = TConfig(
**{
"name": "set & delete cookies.",
"base_url": "https://postman-echo.com",
"variables": {"foo1": "bar1", "foo2": "bar2"},
"verify": False,
"export": ["cookie_foo1", "cookie_foo3"],
}
)
teststeps = [
TStep(
**{
"name": "set cookie foo1 & foo2 & foo3",
"variables": {"foo3": "bar3"},
"request": {
"method": "GET",
"url": "/cookies/set",
"params": {"foo1": "bar111", "foo2": "$foo2", "foo3": "$foo3"},
"headers": {"User-Agent": "HttpRunner/${get_httprunner_version()}"},
},
"extract": {
"cookie_foo1": "$.cookies.foo1",
"cookie_foo3": "$.cookies.foo3",
},
"validate": [
{"eq": ["status_code", 200]},
{"eq": ["$.cookies.foo3", "$foo3"]},
],
}
),
TStep(
**{
"name": "delete cookie foo2",
"request": {
"method": "GET",
"url": "/cookies/delete?foo2",
"headers": {"User-Agent": "HttpRunner/${get_httprunner_version()}"},
},
"validate": [
{"eq": ["status_code", 200]},
{"ne": ["$.cookies.foo1", "$foo1"]},
{"eq": ["$.cookies.foo1", "$cookie_foo1"]},
{"eq": ["$.cookies.foo3", "$cookie_foo3"]},
],
}
),
]
def test_start(self):
s = requests.Session()
HttpRunner(self.config, self.teststeps, session=s).with_variables(
foo1="bar123", foo2="bar22"
).run()

View File

@@ -1,59 +0,0 @@
# NOTICE: Generated By HttpRunner. DO NOT EDIT!
# FROM: examples/postman_echo/cookie_manipulation/set_delete_cookies.yml
from httprunner import HttpRunner, TConfig, TStep
class TestCaseSetDeleteCookies(HttpRunner):
config = TConfig(
**{
"name": "set & delete cookies.",
"variables": {"foo1": "bar1", "foo2": "bar2"},
"base_url": "https://postman-echo.com",
"verify": False,
"export": ["cookie_foo1", "cookie_foo3"],
"path": "examples/postman_echo/cookie_manipulation/set_delete_cookies_test.py",
}
)
teststeps = [
TStep(
**{
"name": "set cookie foo1 & foo2 & foo3",
"variables": {"foo3": "bar3"},
"request": {
"method": "GET",
"url": "/cookies/set",
"params": {"foo1": "bar111", "foo2": "$foo2", "foo3": "$foo3"},
"headers": {"User-Agent": "HttpRunner/${get_httprunner_version()}"},
},
"extract": {
"cookie_foo1": "$.cookies.foo1",
"cookie_foo3": "$.cookies.foo3",
},
"validate": [
{"eq": ["status_code", 200]},
{"ne": ["$.cookies.foo3", "$foo3"]},
],
}
),
TStep(
**{
"name": "delete cookie foo2",
"request": {
"method": "GET",
"url": "/cookies/delete?foo2",
"headers": {"User-Agent": "HttpRunner/${get_httprunner_version()}"},
},
"validate": [
{"eq": ["status_code", 200]},
{"ne": ["$.cookies.foo1", "$foo1"]},
{"eq": ["$.cookies.foo1", "$cookie_foo1"]},
{"eq": ["$.cookies.foo3", "$cookie_foo3"]},
],
}
),
]
if __name__ == "__main__":
TestCaseSetDeleteCookies().test_start()

View File

@@ -0,0 +1,61 @@
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

View File

@@ -57,17 +57,6 @@ if __name__ == "__main__":
)
def __ensure_file_name(path: Text) -> Text:
""" ensure file name not startswith digit
testcases/19.json => testcases/T19.json
"""
filename = os.path.basename(path)
if filename[0] in string.digits:
path = os.path.join(os.path.dirname(path), f"T{filename}")
return path
def __ensure_absolute(path: Text) -> Text:
project_meta = load_project_meta(path)
@@ -108,32 +97,53 @@ def __ensure_testcase_module(path: Text) -> NoReturn:
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 ensure_file_path_valid(file_path: Text) -> Text:
""" ensure file path valid for pytest
Args:
file_path: absolute or relative file path
def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]:
"""convert single YAML/JSON testcase path to python file"""
testcase_path = __ensure_file_name(testcase_path)
raw_file_name, file_suffix = os.path.splitext(os.path.basename(testcase_path))
Returns:
ensured valid absolute file path
"""
raw_file_name, file_suffix = os.path.splitext(file_path)
file_suffix = file_suffix.lower()
if file_suffix not in [".json", ".yml", ".yaml", ".har"]:
raise exceptions.ParamsError(
"testcase file should have .yaml/.yml/.json suffix"
"testcase/testsuite file should have .yaml/.yml/.json/.har suffix"
)
file_name = raw_file_name.replace(" ", "_").replace(".", "_").replace("-", "_")
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")
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)
dir_path = os.path.dirname(testcase_new_path)
file_name, _ = os.path.splitext(os.path.basename(testcase_new_path))
testcase_python_path = os.path.join(dir_path, f"{file_name}_test.py")
# convert title case, e.g. request_with_variables => RequestWithVariables
name_in_title_case = file_name.title().replace("_", "")
@@ -273,10 +283,10 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
# validate testcase format
load_testcase(testcase)
testcase_path = __ensure_absolute(testcase["config"]["path"])
logger.info(f"start to make testcase: {testcase_path}")
testcase_abs_path = __ensure_absolute(testcase["config"]["path"])
logger.info(f"start to make testcase: {testcase_abs_path}")
testcase_python_path, testcase_cls_name = convert_testcase_path(testcase_path)
testcase_python_path, testcase_cls_name = convert_testcase_path(testcase_abs_path)
if dir_path:
testcase_python_path = os.path.join(
dir_path, os.path.basename(testcase_python_path)
@@ -293,7 +303,7 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
config.setdefault("variables", {})
if isinstance(config["variables"], Text):
# get variables by function, e.g. ${get_variables()}
project_meta = load_project_meta(testcase_path)
project_meta = load_project_meta(testcase_abs_path)
config["variables"] = parse_data(
config["variables"], {}, project_meta.functions
)
@@ -327,7 +337,7 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
data = {
"version": __version__,
"testcase_path": __ensure_cwd_relative(testcase_path),
"testcase_path": __ensure_cwd_relative(testcase_abs_path),
"class_name": f"TestCase{testcase_cls_name}",
"imports_list": imports_list,
"config_chain_style": make_config_chain_style(config),
@@ -337,6 +347,10 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
}
content = __TEMPLATE__.render(data)
# ensure new file's directory exists
dir_path = os.path.dirname(testcase_python_path)
os.makedirs(dir_path, exist_ok=True)
with open(testcase_python_path, "w", encoding="utf-8") as f:
f.write(content)
@@ -354,7 +368,7 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
load_testsuite(testsuite)
testsuite_config = testsuite["config"]
testsuite_path = __ensure_absolute(testsuite_config["path"])
testsuite_path = testsuite_config["path"]
testsuite_variables = testsuite_config.get("variables", {})
if isinstance(testsuite_variables, Text):
@@ -367,15 +381,10 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
logger.info(f"start to make testsuite: {testsuite_path}")
# create directory with testsuite file name, put its testcases under this directory
testsuite_file_name, file_suffix = os.path.splitext(testsuite_path)
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"
)
testsuite_dir = __ensure_dir_path_valid(testsuite_file_name)
testsuite_path = ensure_file_path_valid(testsuite_path)
testsuite_dir, file_suffix = os.path.splitext(testsuite_path)
# demo_testsuite.yml => demo_testsuite_yml
testsuite_dir = f"{testsuite_dir}_{file_suffix.lstrip('.')}"
for testcase in testsuite["testcases"]:
# get referenced testcase content

View File

@@ -142,30 +142,30 @@ class TestCompat(unittest.TestCase):
)
def test_ensure_cli_args(self):
args1 = ["examples/postman_echo/request_methods/hardcode.yml", "--failfast"]
args1 = ["examples/postman-echo/request.methods/hardcode.yml", "--failfast"]
self.assertEqual(
compat.ensure_cli_args(args1),
["examples/postman_echo/request_methods/hardcode.yml"],
["examples/postman-echo/request.methods/hardcode.yml"],
)
args2 = ["examples/postman_echo/request_methods/hardcode.yml", "--save-tests"]
args2 = ["examples/postman-echo/request.methods/hardcode.yml", "--save-tests"]
self.assertEqual(
compat.ensure_cli_args(args2),
["examples/postman_echo/request_methods/hardcode.yml"],
["examples/postman-echo/request.methods/hardcode.yml"],
)
self.assertTrue(
os.path.isfile("examples/postman_echo/request_methods/conftest.py")
)
args3 = [
"examples/postman_echo/request_methods/hardcode.yml",
"examples/postman-echo/request.methods/hardcode.yml",
"--report-file",
"report.html",
]
self.assertEqual(
compat.ensure_cli_args(args3),
[
"examples/postman_echo/request_methods/hardcode.yml",
"examples/postman-echo/request.methods/hardcode.yml",
"--html",
"report.html",
"--self-contained-html",
@@ -173,7 +173,7 @@ class TestCompat(unittest.TestCase):
)
args4 = [
"examples/postman_echo/request_methods/hardcode.yml",
"examples/postman-echo/request.methods/hardcode.yml",
"--failfast",
"--save-tests",
"--report-file",
@@ -182,7 +182,7 @@ class TestCompat(unittest.TestCase):
self.assertEqual(
compat.ensure_cli_args(args4),
[
"examples/postman_echo/request_methods/hardcode.yml",
"examples/postman-echo/request.methods/hardcode.yml",
"--html",
"report.html",
"--self-contained-html",

View File

@@ -6,7 +6,7 @@ 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"
path = "examples/postman-echo/request.methods/request_with_variables.yml"
testcase_obj = loader.load_testcase_file(path)
self.assertEqual(
testcase_obj.config.name, "request methods testcase with variables"

View File

@@ -8,12 +8,13 @@ from httprunner.make import (
make_config_chain_style,
make_teststep_chain_style,
pytest_files_run_set,
ensure_file_path_valid,
)
class TestMake(unittest.TestCase):
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_list = main_make(path)
self.assertEqual(
testcase_python_list[0],
@@ -25,7 +26,7 @@ class TestMake(unittest.TestCase):
def test_make_testcase_with_ref(self):
path = [
"examples/postman_echo/request_methods/request_with_testcase_reference.yml"
"examples/postman-echo/request.methods/request_with_testcase_reference.yml"
]
pytest_files_made_cache_mapping.clear()
pytest_files_run_set.clear()
@@ -56,7 +57,7 @@ from examples.postman_echo.request_methods.request_with_functions_test import (
)
def test_make_testcase_folder(self):
path = ["examples/postman_echo/request_methods/"]
path = ["examples/postman-echo/request.methods/"]
testcase_python_list = main_make(path)
self.assertIn(
os.path.join(
@@ -66,42 +67,58 @@ 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")[0], "mubu_login_test.py"
convert_testcase_path("mubu.login.yml"),
(os.path.join(os.getcwd(), "mubu_login_test.py"), "MubuLogin"),
)
self.assertEqual(
convert_testcase_path("/path/to/mubu.login.yml")[0],
"/path/to/mubu_login_test.py",
convert_testcase_path(os.path.join(os.getcwd(), "path/to/mubu.login.yml")),
(os.path.join(os.getcwd(), "path/to/mubu_login_test.py"), "MubuLogin"),
)
self.assertEqual(
convert_testcase_path("/path/to 2/mubu.login.yml")[0],
"/path/to 2/mubu_login_test.py",
convert_testcase_path("path/to 2/mubu.login.yml"),
(os.path.join(os.getcwd(), "path/to_2/mubu_login_test.py"), "MubuLogin"),
)
self.assertEqual(
convert_testcase_path("/path/to 2/mubu.login.yml")[1], "MubuLogin"
convert_testcase_path("path/to-2/mubu login.yml"),
(os.path.join(os.getcwd(), "path/to_2/mubu_login_test.py"), "MubuLogin"),
)
self.assertEqual(
convert_testcase_path("mubu login.yml")[0], "mubu_login_test.py"
convert_testcase_path("path/to.2/幕布login.yml"),
(os.path.join(os.getcwd(), "path/to_2/幕布login_test.py"), "幕布Login"),
)
self.assertEqual(
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",
)
self.assertEqual(
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")
def test_make_testsuite(self):
path = ["examples/postman_echo/request_methods/demo_testsuite.yml"]
path = ["examples/postman-echo/request.methods/demo_testsuite.yml"]
pytest_files_made_cache_mapping.clear()
pytest_files_run_set.clear()
testcase_python_list = main_make(path)

View File

@@ -9,7 +9,7 @@ class TestHttpRunner(unittest.TestCase):
def test_run_testcase_by_path_request_only(self):
self.runner.run_path(
"examples/postman_echo/request_methods/request_with_functions.yml"
"examples/postman-echo/request.methods/request_with_functions.yml"
)
result = self.runner.get_summary()
self.assertTrue(result.success)
@@ -19,7 +19,7 @@ class TestHttpRunner(unittest.TestCase):
def test_run_testcase_by_path_ref_testcase(self):
self.runner.run_path(
"examples/postman_echo/request_methods/request_with_testcase_reference.yml"
"examples/postman-echo/request.methods/request_with_testcase_reference.yml"
)
result = self.runner.get_summary()
self.assertTrue(result.success)