httprunner3源码解读(2)models.py

我们首先来看下models.py的代码结构

httprunner3源码解读(2)models.py


我们可以看到这个模块中定义了12个属性和22个模型类,我们依次来看
 

属性源码分析 import os from enum import Enum from typing import Any from typing import Dict, Text, Union, Callable from typing import List from pydantic import BaseModel, Field from pydantic import HttpUrl Name = Text Url = Text BaseUrl = Union[HttpUrl, Text] VariablesMapping = Dict[Text, Any] FunctionsMapping = Dict[Text, Callable] Headers = Dict[Text, Text] Cookies = Dict[Text, Text] Verify = bool Hooks = List[Union[Text, Dict[Text, Text]]] Export = List[Text] Validators = List[Dict] Env = Dict[Text, Any]

一句话总结:用到了typing和pydantic模块,目的是告诉读者我这些属性是什么类型的
 

模型类源码分析

这里以代码注解的方式讲解
 

MethodEnum class MethodEnum(Text, Enum): """ 枚举请求方法,定义了常用的http请求方法 """ GET = "GET" POST = "POST" PUT = "PUT" DELETE = "DELETE" HEAD = "HEAD" OPTIONS = "OPTIONS" PATCH = "PATCH

 

TConfig class TConfig(BaseModel): """ 定义配置信息,包含如下: 1.name (str) 2.verify (bool) 3.base_url (http/https开头的str类型) 4.variables (dict) 5.parameters(dict) 6.export (list[str]) 7.path (str) 8.weight (int) """ name: Name # str类型 verify: Verify = False base_url: BaseUrl = "" # Text: prepare variables in debugtalk.py, ${gen_variables()} variables: Union[VariablesMapping, Text] = {} parameters: Union[VariablesMapping, Text] = {} # setup_hooks: Hooks = [] # teardown_hooks: Hooks = [] export: Export = [] path: Text = None weight: int = 1

 

TRequest class TRequest(BaseModel): """ requests.Request model 1.method (枚举类型) 2.url (str) 3.params (dict) 4.headers (dict) 5.req_json(dict/list/str) 6.data (dict/str) 7.cookie (dict) 8.timeout (float) 9.allow_redirects (bool) 10.verify (bool) 11.upload (dict) """ method: MethodEnum url: Url params: Dict[Text, Text] = {} headers: Headers = {} req_json: Union[Dict, List, Text] = Field(None, alias="json") data: Union[Text, Dict[Text, Any]] = None cookies: Cookies = {} timeout: float = 120 allow_redirects: bool = True verify: Verify = False upload: Dict = {} # used for upload files

 

TStep class TStep(BaseModel): """ 测试步骤,里面包含了request请求 1.name (str) 2.request (TRequest) 3.testcase (str/Callable) 4.variables (dict) 5.setup_hooks (list(dict)) 6.teardown_hooks (list(dict)) 7.extract (dict) 8.export (list) 9.validators (list(dict)) 10.validate_script (list[str]) """ name: Name request: Union[TRequest, None] = None testcase: Union[Text, Callable, None] = None variables: VariablesMapping = {} setup_hooks: Hooks = [] teardown_hooks: Hooks = [] # used to extract request's response field extract: VariablesMapping = {} # used to export session variables from referenced testcase export: Export = [] validators: Validators = Field([], alias="validate") validate_script: List[Text] = []

 

TestCase class TestCase(BaseModel): """ 测试用例,包含了测试步骤和配置信息 """ config: TConfig teststeps: List[TStep]

 

ProjectMeta class ProjectMeta(BaseModel): """ 项目结构 1.debugtalk_py (str) debugtakl文件内容 2.debugtalk_path (str) debugtalk文件路径 3.dot_env_path (str) env文件路径 4.functions (dict(Callable/str)) 在debugtalk中定义的函数 5.env (dict) 环境 6.RootDir (str) 根路径(绝对路径),debugtalk位于的路径 """ debugtalk_py: Text = "" # debugtalk.py file content debugtalk_path: Text = "" # debugtalk.py file path dot_env_path: Text = "" # .env file path functions: FunctionsMapping = {} # functions defined in debugtalk.py env: Env = {} RootDir: Text = os.getcwd() # project root directory (ensure absolute), the path debugtalk.py located

 

TestsMapping class TestsMapping(BaseModel): """ 测试映射 1.project_meta 2.testcases 测试用例集,list下有多个用例 """ project_meta: ProjectMeta testcases: List[TestCase]

 

TestCaseTime class TestCaseTime(BaseModel): """ 测试用例时间 1.start_at:开始时间默认为0 2.start_at_iso_format:以iso格式启动 3.duration:持续时间 """ start_at: float = 0 start_at_iso_format: Text = "" duration: float = 0

 

TestCaseInOut class TestCaseInOut(BaseModel): """ 测试用例的输入输出: config_vars:配置变量 export_vars:导出变量 """ config_vars: VariablesMapping = {} export_vars: Dict = {}

 

RequestStat class RequestStat(BaseModel): """ 请求指标: content_size:内容大小 response_time_ms:响应时间(ms) elapsed_ms:逝去的时间(ms) """ content_size: float = 0 response_time_ms: float = 0 elapsed_ms: float = 0

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwzgfs.html