原始碼目錄結構
我們首先來看下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
AddressData
class AddressData(BaseModel):
"""
客戶端與伺服器地址資料
client_ip:客戶端ip地址
client_port:客戶端埠號
server_ip:伺服器ip地址
server_port:伺服器埠號
"""
client_ip: Text = "N/A"
client_port: int = 0
server_ip: Text = "N/A"
server_port: int = 0
RequestData
class RequestData(BaseModel):
"""
請求資料
method:請求方法,預設為GET
url:url地址
headers:請求頭
cookies:cookie資訊
body:請求體
"""
method: MethodEnum = MethodEnum.GET
url: Url
headers: Headers = {}
cookies: Cookies = {}
body: Union[Text, bytes, List, Dict, None] = {}
ResponseData
class ResponseData(BaseModel):
"""
響應資料
status_code:狀態碼
headers:響應頭
cookies:cookie資訊
encoding:編碼格式
content_type:內容型別
body:響應體
"""
status_code: int
headers: Dict
cookies: Cookies
encoding: Union[Text, None] = None
content_type: Text
body: Union[Text, bytes, List, Dict]
ReqRespData
class ReqRespData(BaseModel):
"""
請求響應資料
request:RequestData
response:ResponseData
"""
request: RequestData
response: ResponseData
SessionData
class SessionData(BaseModel):
"""
request session data, including request, response, validators and stat data
"""
success: bool = False
# in most cases, req_resps only contains one request & response
# while when 30X redirect occurs, req_resps will contain multiple request & response
req_resps: List[ReqRespData] = []
stat: RequestStat = RequestStat()
address: AddressData = AddressData()
validators: Dict = {}
StepData
class StepData(BaseModel):
"""
teststep data, each step maybe corresponding to one request or one testcase
測試步驟資料,每個步驟可能對應一個請求或一個測試用例
"""
success: bool = False
name: Text = "" # teststep name
data: Union[SessionData, List['StepData']] = None
export_vars: VariablesMapping = {}
TestCaseSummary
class TestCaseSummary(BaseModel):
"""
測試用例結果
name:測試用例名字
success:測試用例成功的狀態
case_id:測試用例的id
time:測試用例的時間
in_out:測試用例的匯入匯出資料
log:測試用例的日誌
step_datas:測試步驟的資料
"""
name: Text
success: bool
case_id: Text
time: TestCaseTime
in_out: TestCaseInOut = {}
log: Text = ""
step_datas: List[StepData] = []
PlatformInfo
class PlatformInfo(BaseModel):
"""
平臺資訊
httprunner_version:httprunner版本號
python_version:python版本
platform:平臺
"""
httprunner_version: Text
python_version: Text
platform: Text
TestCaseRef
class TestCaseRef(BaseModel):
"""
包含testcase
"""
name: Text
base_url: Text = ""
testcase: Text
variables: VariablesMapping = {}
TestSuite
class TestSuite(BaseModel):
"""
測試套件
TestSuite包含TestCaseRef
TestCaseRef包含testcase
"""
config: TConfig
testcases: List[TestCaseRef]
Stat
class Stat(BaseModel):
"""
統計資訊
total:總數
success:成功的用例數
fail:失敗的用例數
"""
total: int = 0
success: int = 0
fail: int = 0
TestSuiteSummary
class TestSuiteSummary(BaseModel):
"""
測試套件結果
success:成功的狀態
stat:統計資訊
time:測試用例花費的時間
platform:平臺資訊
testcases:測試用例集
"""
success: bool = False
stat: Stat = Stat()
time: TestCaseTime = TestCaseTime()
platform: PlatformInfo
testcases: List[TestCaseSummary]