Skip to content

Commit

Permalink
更新用例数据架构验证为严格模式 (#27)
Browse files Browse the repository at this point in the history
* 更新用例数据架构验证为严格模式
  • Loading branch information
wu-clan authored Aug 24, 2023
1 parent d68d580 commit 079ceec
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 20 deletions.
4 changes: 2 additions & 2 deletions httpfpt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def testcase_date_verify(verify: str) -> None:
file_list = search_all_case_yaml_files()
for file in file_list:
file_data = read_yaml(None, filename=file)
CaseData.model_validate(file_data, strict=True)
CaseData.model_validate(file_data)
else:
typer.secho(f'🔥 开始验证 {verify} 测试数据结构...', fg='cyan', bold=True)
file_data = read_yaml(None, filename=verify)
CaseData.model_validate(file_data, strict=True)
CaseData.model_validate(file_data)
except ValidationError as e:
count = e.error_count()
msg += str(e)
Expand Down
2 changes: 1 addition & 1 deletion httpfpt/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ minversion = 7.0.0
; Pytest plugin requirements
required_plugins = pytest-html>=2.0.0 pytest-xdist>=2.5.0
; Collapse all tables of html report
render_collapsed = True
render_collapsed = all
; Logging settings
log_cli = False
log_cli_format = %(asctime)s | %(levelname)s | %(message)s
Expand Down
13 changes: 8 additions & 5 deletions httpfpt/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import datetime
import os
import shutil
from typing import Union, Optional
from typing import Union, Optional, Literal

import pytest

Expand All @@ -26,7 +26,7 @@

def run(
# log level
log_level: str = '-v',
log_level: Literal['-q', '-s', '-v', '-vs'] = '-v',
# case path
case_path: Optional[str] = None,
# html report
Expand All @@ -39,8 +39,8 @@ def run(
reruns: int = 0,
maxfail: int = 0,
x: bool = False,
n: Union[str, int] = 'auto',
dist: str = 'loadscope',
n: Union[Literal['auto', 'logical'], int] = 'auto',
dist: Literal['load', 'loadscope', 'loadfile', 'loadgroup', 'worksteal', 'no'] = 'loadscope',
strict_markers: bool = False,
capture: bool = True,
disable_warnings: bool = True,
Expand Down Expand Up @@ -176,9 +176,12 @@ def run(
if __name__ == '__main__':
# 初始化 redis 数据库 (必选)
redis_client.init()

# 用例数据唯一 case_id 检测(可选)
get_all_testcase_id(get_all_testcase_data())
# 用例数据 pydantic 检测(可选)

# 用例数据完整架构 pydantic 快速检测(可选)
get_all_testcase_data(pydantic_verify=True)

# 执行程序 (必选)
run()
7 changes: 6 additions & 1 deletion httpfpt/schemas/case_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# -*- coding: utf-8 -*-
from typing import Optional, Union, Tuple, List, Dict, Any, Literal

from pydantic import BaseModel, Field, AnyHttpUrl
from pydantic import BaseModel, Field, AnyHttpUrl, ConfigDict

__all__ = ['CaseData']


class ConfigAllureData(BaseModel):
Expand Down Expand Up @@ -89,5 +91,8 @@ class Steps(BaseModel):


class CaseData(BaseModel):
model_config = ConfigDict(extra='forbid', strict=True)

config: Config
test_steps: Union[Steps, List[Steps]]
filename: Optional[str] = None
6 changes: 3 additions & 3 deletions httpfpt/utils/data_manage/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from httpfpt.common.yaml_handler import read_yaml
from httpfpt.schemas.case_data import CaseData
from httpfpt.utils.file_control import search_all_case_yaml_files
from httpfpt.utils.pydantic_error_parse import parse_error
from httpfpt.utils.pydantic_parser import parse_error


class GitRepoPaser:
Expand Down Expand Up @@ -48,8 +48,8 @@ def import_git_to_local(src: str) -> None:
for case_data in all_case_data:
try:
count: int = 0
CaseData.model_validate(case_data, strict=True)
CaseData.model_validate(case_data)
except ValidationError as e:
count = parse_error(e)
if count > 0:
raise ValueError(f'用例数据校验失败,共有 {count} 处错误, 错误详情请查看日志')
raise ValueError(f'导入 Git 用例数据校验失败,共有 {count} 处错误, 错误详情请查看日志')
File renamed without changes.
2 changes: 1 addition & 1 deletion httpfpt/utils/relate_testcase_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from httpfpt.schemas.case_data import CaseData
from httpfpt.utils.allure_control import allure_step
from httpfpt.utils.file_control import search_all_case_yaml_files, get_file_property
from httpfpt.utils.pydantic_error_parse import parse_error
from httpfpt.utils.pydantic_parser import parse_error
from httpfpt.utils.request.request_data_parse import RequestDataParse
from httpfpt.utils.request.vars_extractor import VarsExtractor

Expand Down
10 changes: 5 additions & 5 deletions httpfpt/utils/request/case_data_file_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
from pydantic import ValidationError

from httpfpt.schemas.case_data import CaseData
from httpfpt.utils.pydantic_error_parse import parse_error
from httpfpt.utils.pydantic_parser import parse_error


def get_request_data(*, file_data: dict, use_pydantic_verify: bool = False) -> Union[List[Dict[str, Any]], None]:
"""
通过解析读取的测试用例文件数据, 获取用于数据驱动的请求数据
:param file_data: 从测试用例数据文件读取的测试用例数据
:param use_pydantic_verify: pydantic 数据验证开关
:param use_pydantic_verify: pydantic 数据架构验证
:return:
"""
try:
Expand All @@ -35,7 +35,7 @@ def get_request_data(*, file_data: dict, use_pydantic_verify: bool = False) -> U
count: int = 0
if isinstance(cases, dict):
if use_pydantic_verify:
return [CaseData(**file_data).model_dump(by_alias=True)]
return [CaseData(**file_data).model_dump()]
else:
return [file_data]
elif isinstance(cases, list):
Expand All @@ -46,7 +46,7 @@ def get_request_data(*, file_data: dict, use_pydantic_verify: bool = False) -> U
data = copy.deepcopy(file_data)
data.update(test_steps)
if use_pydantic_verify:
case_list.append(CaseData(**data).model_dump(by_alias=True))
case_list.append(CaseData(**data).model_dump())
else:
case_list.append(data)
else:
Expand All @@ -56,4 +56,4 @@ def get_request_data(*, file_data: dict, use_pydantic_verify: bool = False) -> U
count = parse_error(e)

if count > 0:
raise ValueError('请求测试用例数据格式错误, 请检查测试用例文件内容,错误详情请查看日志文件')
raise ValueError(f'测试用例数据校验失败,共有 {count} 处错误, 错误详情请查看日志')
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ jsonpath==0.82
loguru==0.6.0
openpyxl==3.0.7
pre-commit==3.2.2
pydantic==2.1.1
pydantic==2.3.0
pymysql==0.9.3
pyright==1.1.315
pytest==7.2.2
pytest-html==4.0.0rc5
pytest-html==4.0.0rc6
pytest-loguru==0.2.0
pytest-metadata==3.0.0
pytest-pretty==1.2.0
Expand Down

0 comments on commit 079ceec

Please sign in to comment.