feat 当HTTP请求未设置期望断言,但HTTP状态码返回4xx/5xx时,结果置为失败

This commit is contained in:
zhangzheng 2024-09-07 13:32:22 +08:00
parent 9fa122bc5f
commit 2a0fbda88b
5 changed files with 39 additions and 15 deletions

View File

@ -5,9 +5,10 @@ import json
from app.cores.dictionaries import TEST_FIELD, MATCHING_RULE, EXPECTATION_LOGIC
from app.cores.assertion import AssertEqual, AssertSubString, AssertContains, AssertMatches, AssertXPath
from app.cores.case.http.request import HTTPRequest
def get_expectations_result(expectations, request, expectation_logic) -> bool:
def get_expectations_result(expectations, request: HTTPRequest, expectation_logic) -> bool:
"""
获取期望结果
:param expectations: 当前请求案例中所有期望/断言数据, 如果没有设置期望则应该是一个空list会在执行后将结果更新到表中
@ -16,6 +17,7 @@ def get_expectations_result(expectations, request, expectation_logic) -> bool:
:return: 只要有一个断言失败则返回False全部成功则返回True
"""
rets = []
response_error_code = request.is_response_error_code() # 应答是否返回错误码4xx/5xx
if expectations:
for expectation in expectations:
actual_value = _get_actual_value_from_test_field(test_field=expectation.test_field, request=request)
@ -26,17 +28,20 @@ def get_expectations_result(expectations, request, expectation_logic) -> bool:
negater=expectation.negater,
)
rets.append(last_result)
# 如果断言了状态码并且断言成功,则忽略应答的状态码
if last_result is True and expectation.test_field == TEST_FIELD.RESPONSE_CODE:
response_error_code = False
# 解决HTTP响应文本内容包含html片段导致前台解析问题 begin
if last_result is False and expectation.test_field == TEST_FIELD.TEXT_RESPONSE:
last_failure_msg = '[期望值: %s] [实际值: 请参考“响应-响应体”中内容]' % expectation.value_
# 解决HTTP响应文本内容包含html片段导致前台解析问题 end
expectation.update_assert_result(last_result=last_result, last_failure_msg=last_failure_msg)
if len(rets) == 0: # 未设置期望断言则结果为True
return True
return True & (not response_error_code)
if expectation_logic == EXPECTATION_LOGIC.AND:
return all(rets)
return all(rets) & (not response_error_code)
if expectation_logic == EXPECTATION_LOGIC.OR:
return any(rets)
return any(rets) & (not response_error_code)
def _get_actual_value_from_test_field(test_field, request):

View File

@ -36,6 +36,8 @@ class HTTPCaseDispatcher(AbstractCaseDispatcher):
self.postprocessor_failure = None
# 后处理脚本错误信息
self.postprocessor_failure_message = None
# 后处理脚本内容
self.postprocessor_script: str = ''
# 组件期望断言结果
self.expectations_result = False

View File

@ -95,6 +95,12 @@ class HTTPRequest:
self._handle_response()
return self.response
def is_response_error_code(self) -> bool:
"""
HTTP请求是否返回的是错误码 4xx 5xx
"""
return not self.response.ok
def _handle_response(self):
"""处理应答解析拿到 应答体 应答头 请求体 请求头"""
self._handle_response_status_code()

View File

@ -151,6 +151,7 @@ def execute_http_case():
'request_headers': json.dumps(request_.request_headers, ensure_ascii=False),
'assert_success': all([expectations_result, not case.specific_case.postprocessor_failure]), # 期望断言结果和后处理断言结果
'expectations_result': expectations_result,
'postprocessor_script': case.specific_case.postprocessor_script,
'postprocessor_failure': case.specific_case.postprocessor_failure,
'postprocessor_failure_message': case.specific_case.postprocessor_failure_message,
'expectations': json.loads(render_to_json(case.specific_case.expectations)),

View File

@ -1092,21 +1092,28 @@ function getBaseCaseElement(case_id, case_type) {
// 根据期望结果进行前台展示提示信息
if (data.assert_success){
element.dom.$div_assert_result_success_hint.css('display', '');
switchExpectationResultToSuccess();
switchPostprocessorResultToSuccess();
}else{
if (!data.expectations_result){ // 期望断言失败
if (has_expectations){
switchExpectationResultToFailure();
}else{ // 未设置期望
element.dom.$div_expectation_not_set_hint.css('display', '');
}
}else{ // 期望断言成功
if (has_expectations) { // 如果设置有期望断言
switchExpectationResultToSuccess();
}
if (data.postprocessor_script.length > 0) { // 如果有设置后处理脚本
switchPostprocessorResultToSuccess();
}
}else {
if (!data.expectations_result){ // 期望断言失败
switchExpectationResultToFailure();
// if (has_expectations){
// switchExpectationResultToFailure();
// }else{ // 未设置期望
// switchExpectationResultToNotSet();
// }
}else{ // 期望断言成功
if (has_expectations){
switchExpectationResultToSuccess();
}
}
if (data.postprocessor_failure){ // 后处理断言失败
switchPostprocessorResultToFailure(data.postprocessor_failure_message);
}else{ // 后处理成功
}else if (data.postprocessor_script.length > 0) { // 如果有设置后处理脚本
switchPostprocessorResultToSuccess();
}
}
@ -1189,6 +1196,9 @@ function getBaseCaseElement(case_id, case_type) {
element.dom.$pills_expectation_tab.addClass('failure');
element.dom.$div_expectation_result_failure_hint.css('display', '');
}
function switchExpectationResultToNotSet() {
element.dom.$div_expectation_not_set_hint.css('display', '');
}
// 后处理脚本断言失败样式设置
function switchPostprocessorResultToFailure(postprocessor_failure_message) {
element.dom.$pills_postprocessor_script_tab.addClass('failure');