feat: support alter response in teardown hooks

This commit is contained in:
debugtalk
2020-06-07 19:04:06 +08:00
parent 9367cac706
commit 5c06b91b62
18 changed files with 54 additions and 32 deletions

View File

@@ -5,6 +5,7 @@
**Added** **Added**
- feat: implement step setup/teardown hooks - feat: implement step setup/teardown hooks
- feat: support alter response in teardown hooks
**Fixed** **Fixed**

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/httpbin/basic.yml # FROM: examples/httpbin/basic.yml
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

View File

@@ -126,7 +126,7 @@ def setup_hook_httpntlmauth(request):
def alter_response(response): def alter_response(response):
response.status_code = 500 response.status_code = 500
response.headers["Content-Type"] = "html/text" response.headers["Content-Type"] = "html/text"
response.json["headers"]["Host"] = "127.0.0.1:8888" response.body["headers"]["Host"] = "127.0.0.1:8888"
response.new_attribute = "new_attribute_value" response.new_attribute = "new_attribute_value"
response.new_attribute_dict = {"key": 123} response.new_attribute_dict = {"key": 123}

View File

@@ -31,7 +31,7 @@ teststeps:
teardown_hooks: teardown_hooks:
- ${alter_response($response)} - ${alter_response($response)}
validate: validate:
- eq: ["status_code", 200] - eq: ["status_code", 500]
# TODO: implement hooks - eq: [headers."Content-Type", "html/text"]
# - eq: [body.headers."Content-Type", "html/text"] - eq: [body.headers."Content-Type", "application/json"]
- eq: [body.headers.Host, "httpbin.org"] - eq: [body.headers.Host, "127.0.0.1:8888"]

View File

@@ -24,8 +24,10 @@ class TestCaseHooks(HttpRunner):
.get("/headers") .get("/headers")
.teardown_hook("${alter_response($response)}") .teardown_hook("${alter_response($response)}")
.validate() .validate()
.assert_equal("status_code", 200) .assert_equal("status_code", 500)
.assert_equal("body.headers.Host", "httpbin.org") .assert_equal('headers."Content-Type"', "html/text")
.assert_equal('body.headers."Content-Type"', "application/json")
.assert_equal("body.headers.Host", "127.0.0.1:8888")
), ),
] ]

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/httpbin/load_image.yml # FROM: examples/httpbin/load_image.yml
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/httpbin/upload.yml # FROM: examples/httpbin/upload.yml
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/httpbin/validate.yml # FROM: examples/httpbin/validate.yml
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/postman_echo/request_methods/request_with_functions.yml # FROM: examples/postman_echo/request_methods/request_with_functions.yml
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/postman_echo/request_methods/request_with_testcase_reference.yml # FROM: examples/postman_echo/request_methods/request_with_testcase_reference.yml
import os import os

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/postman_echo/request_methods/hardcode.yml # FROM: examples/postman_echo/request_methods/hardcode.yml
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/postman_echo/request_methods/request_with_functions.yml # FROM: examples/postman_echo/request_methods/request_with_functions.yml
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/postman_echo/request_methods/request_with_testcase_reference.yml # FROM: examples/postman_echo/request_methods/request_with_testcase_reference.yml
import os import os

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/postman_echo/request_methods/request_with_variables.yml # FROM: examples/postman_echo/request_methods/request_with_variables.yml
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/postman_echo/request_methods/validate_with_functions.yml # FROM: examples/postman_echo/request_methods/validate_with_functions.yml
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

View File

@@ -1,4 +1,4 @@
# NOTE: Generated By HttpRunner v3.0.9 # NOTE: Generated By HttpRunner v3.0.10
# FROM: examples/postman_echo/request_methods/validate_with_variables.yml # FROM: examples/postman_echo/request_methods/validate_with_variables.yml
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

View File

@@ -4,6 +4,7 @@ import jmespath
import requests import requests
from loguru import logger from loguru import logger
from httprunner import exceptions
from httprunner.exceptions import ValidationFailure, ParamsError from httprunner.exceptions import ValidationFailure, ParamsError
from httprunner.parser import parse_data, parse_string_value, get_mapping_function from httprunner.parser import parse_data, parse_string_value, get_mapping_function
from httprunner.models import VariablesMapping, Validators, FunctionsMapping from httprunner.models import VariablesMapping, Validators, FunctionsMapping
@@ -109,20 +110,36 @@ class ResponseObject(object):
""" """
self.resp_obj = resp_obj self.resp_obj = resp_obj
try:
body = resp_obj.json()
except ValueError:
body = resp_obj.content
self.resp_obj_meta = {
"status_code": resp_obj.status_code,
"headers": resp_obj.headers,
"cookies": dict(resp_obj.cookies),
"body": body,
}
self.validation_results: Dict = {} self.validation_results: Dict = {}
def __getattr__(self, key):
if key in ["json", "content", "body"]:
try:
value = self.resp_obj.json()
except ValueError:
value = self.resp_obj.content
elif key == "cookies":
value = self.resp_obj.cookies.get_dict()
else:
try:
value = getattr(self.resp_obj, key)
except AttributeError:
err_msg = "ResponseObject does not have attribute: {}".format(key)
logger.error(err_msg)
raise exceptions.ParamsError(err_msg)
self.__dict__[key] = value
return value
@property
def resp_obj_meta(self):
return {
"status_code": self.status_code,
"headers": self.headers,
"cookies": self.cookies,
"body": self.body,
}
def extract(self, extractors: Dict[Text, Text]) -> Dict[Text, Any]: def extract(self, extractors: Dict[Text, Text]) -> Dict[Text, Any]:
if not extractors: if not extractors:
return {} return {}

View File

@@ -265,7 +265,9 @@ class RequestWithOptionalArgs(object):
self.__step_context.request.upload.update(file_info) self.__step_context.request.upload.update(file_info)
return self return self
def teardown_hook(self, hook: Text, assign_var_name: Text = None) -> "RequestWithOptionalArgs": def teardown_hook(
self, hook: Text, assign_var_name: Text = None
) -> "RequestWithOptionalArgs":
if assign_var_name: if assign_var_name:
self.__step_context.teardown_hooks.append({assign_var_name: hook}) self.__step_context.teardown_hooks.append({assign_var_name: hook})
else: else: