feat: get request & response meta datas

This commit is contained in:
debugtalk
2020-04-23 21:53:03 +08:00
parent f3a8bd78ce
commit 27c1ce9772
4 changed files with 42 additions and 9 deletions
@@ -25,5 +25,5 @@ teststeps:
session_foo2: "body.args.foo2" session_foo2: "body.args.foo2"
validate: validate:
- eq: ["status_code", 200] - 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)}"] - less_than: ["body.args.sum_v", "${sum_two(2, 2)}"]
@@ -95,4 +95,5 @@ class TestCaseRequestMethodsValidateWithVariables(TestCaseRunner):
if __name__ == '__main__': if __name__ == '__main__':
TestCaseRequestMethodsValidateWithVariables().run() runner = TestCaseRequestMethodsValidateWithVariables().run()
print(runner.meta_datas)
+17 -6
View File
@@ -1,20 +1,26 @@
from typing import List from typing import List
import requests
from loguru import logger from loguru import logger
from httprunner.client import HttpSession
from httprunner.v3.parser import build_url, parse_data, parse_variables_mapping from httprunner.v3.parser import build_url, parse_data, parse_variables_mapping
from httprunner.v3.response import ResponseObject 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): class TestCaseRunner(object):
config: TestsConfig = {} config: TestsConfig = {}
teststeps: List[TestStep] = [] 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 self.session = s
return self return self
@@ -40,8 +46,8 @@ class TestCaseRunner(object):
logger.debug(f"request kwargs(raw): {parsed_request_dict}") logger.debug(f"request kwargs(raw): {parsed_request_dict}")
# request # request
session = self.session or requests.Session() self.session = self.session or HttpSession()
resp = session.request(method, url, **parsed_request_dict) resp = self.session.request(method, url, **parsed_request_dict)
resp_obj = ResponseObject(resp) resp_obj = ResponseObject(resp)
# extract # extract
@@ -59,6 +65,7 @@ class TestCaseRunner(object):
def test_start(self): def test_start(self):
"""main entrance""" """main entrance"""
self.meta_datas.clear()
session_variables = {} session_variables = {}
for step in self.teststeps: for step in self.teststeps:
# update with config variables # update with config variables
@@ -71,6 +78,10 @@ class TestCaseRunner(object):
extract_mapping = self.run_step(step) extract_mapping = self.run_step(step)
# save extracted variables to session variables # save extracted variables to session variables
session_variables.update(extract_mapping) session_variables.update(extract_mapping)
# save request & response meta data
self.meta_datas.append(self.session.meta_data)
return self
def run(self): def run(self):
"""main entrance alias for test_start""" """main entrance alias for test_start"""
+22 -1
View File
@@ -1,6 +1,7 @@
from enum import Enum from enum import Enum
from typing import Any 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 BaseModel, Field
from pydantic import HttpUrl from pydantic import HttpUrl
@@ -56,7 +57,27 @@ class Request(BaseModel):
class TestStep(BaseModel): class TestStep(BaseModel):
name: Name name: Name
times: int = 1
request: Request request: Request
variables: VariablesMapping = {} variables: VariablesMapping = {}
extract: Dict[Text, Text] = {} extract: Dict[Text, Text] = {}
validators: Validators = Field([], alias="validate") 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]