mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-11 18:11:21 +08:00
feat: get request & response meta datas
This commit is contained in:
@@ -25,5 +25,5 @@ teststeps:
|
||||
session_foo2: "body.args.foo2"
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
- eq: ["body.args.sum_v", "3"]
|
||||
- eq: ["body.args.sum_v", 3]
|
||||
- less_than: ["body.args.sum_v", "${sum_two(2, 2)}"]
|
||||
|
||||
@@ -95,4 +95,5 @@ class TestCaseRequestMethodsValidateWithVariables(TestCaseRunner):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
TestCaseRequestMethodsValidateWithVariables().run()
|
||||
runner = TestCaseRequestMethodsValidateWithVariables().run()
|
||||
print(runner.meta_datas)
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
from typing import List
|
||||
|
||||
import requests
|
||||
from loguru import logger
|
||||
|
||||
from httprunner.client import HttpSession
|
||||
from httprunner.v3.parser import build_url, parse_data, parse_variables_mapping
|
||||
from httprunner.v3.response import ResponseObject
|
||||
from httprunner.v3.schema import TestsConfig, TestStep, VariablesMapping
|
||||
from httprunner.v3.schema import TestsConfig, TestStep, VariablesMapping, TestCase
|
||||
|
||||
|
||||
class TestCaseRunner(object):
|
||||
|
||||
config: TestsConfig = {}
|
||||
teststeps: List[TestStep] = []
|
||||
session: requests.Session = None
|
||||
session: HttpSession = None
|
||||
meta_datas: List = []
|
||||
|
||||
def with_session(self, s: requests.Session) -> "TestCaseRunner":
|
||||
def init(self, testcase: TestCase) -> "TestCaseRunner":
|
||||
self.config = testcase.config
|
||||
self.teststeps = testcase.teststeps
|
||||
return self
|
||||
|
||||
def with_session(self, s: HttpSession) -> "TestCaseRunner":
|
||||
self.session = s
|
||||
return self
|
||||
|
||||
@@ -40,8 +46,8 @@ class TestCaseRunner(object):
|
||||
logger.debug(f"request kwargs(raw): {parsed_request_dict}")
|
||||
|
||||
# request
|
||||
session = self.session or requests.Session()
|
||||
resp = session.request(method, url, **parsed_request_dict)
|
||||
self.session = self.session or HttpSession()
|
||||
resp = self.session.request(method, url, **parsed_request_dict)
|
||||
resp_obj = ResponseObject(resp)
|
||||
|
||||
# extract
|
||||
@@ -59,6 +65,7 @@ class TestCaseRunner(object):
|
||||
|
||||
def test_start(self):
|
||||
"""main entrance"""
|
||||
self.meta_datas.clear()
|
||||
session_variables = {}
|
||||
for step in self.teststeps:
|
||||
# update with config variables
|
||||
@@ -71,6 +78,10 @@ class TestCaseRunner(object):
|
||||
extract_mapping = self.run_step(step)
|
||||
# save extracted variables to session variables
|
||||
session_variables.update(extract_mapping)
|
||||
# save request & response meta data
|
||||
self.meta_datas.append(self.session.meta_data)
|
||||
|
||||
return self
|
||||
|
||||
def run(self):
|
||||
"""main entrance alias for test_start"""
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
from typing import Dict, List, Text, Union, Callable
|
||||
from typing import Dict, Text, Union, Callable
|
||||
from typing import List
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import HttpUrl
|
||||
@@ -56,7 +57,27 @@ class Request(BaseModel):
|
||||
|
||||
class TestStep(BaseModel):
|
||||
name: Name
|
||||
times: int = 1
|
||||
request: Request
|
||||
variables: VariablesMapping = {}
|
||||
extract: Dict[Text, Text] = {}
|
||||
validators: Validators = Field([], alias="validate")
|
||||
|
||||
|
||||
class TestCase(BaseModel):
|
||||
config: TestsConfig
|
||||
teststeps: List[TestStep]
|
||||
|
||||
|
||||
class ProjectMeta(BaseModel):
|
||||
debugtalk_py: Text = ""
|
||||
variables: VariablesMapping = {}
|
||||
functions: FunctionsMapping = {}
|
||||
env: Env = {}
|
||||
PWD: Text
|
||||
test_path: Text
|
||||
|
||||
|
||||
class TestsMapping(BaseModel):
|
||||
project_mapping: ProjectMeta # TODO: rename to project_meta
|
||||
testcases: List[TestCase]
|
||||
|
||||
Reference in New Issue
Block a user