Skip to content

响应对象

什么是响应对象?

响应对象是 AsyncTest 在自定义脚本中提供的 当前接口响应的访问对象,用于在脚本中获取接口响应的各个部分(响应体、响应头、状态码、响应时间、错误信息等)。

你可以通过 at.response 访问当前的响应对象,并通过其暴露的属性和方法来获取响应内容。


基本概念

响应对象提供了两类访问方式:

  1. 异步属性:直接访问响应的各个部分(无括号,但需要 await
  2. 异步方法:对响应内容进行处理和转换(有括号,需要 await

重要提示: 所有属性和方法都是异步的,调用时必须添加 await 关键字。


响应基本信息

async_body

异步属性 - 获取响应体原始内容

python
body = await at.response.async_body

返回值: 响应体字符串

使用场景:

  • 获取接口返回的原始响应内容
  • 在接口后置操作中提取响应数据

示例:

python
# 获取响应体
body = await at.response.async_body
print(f"响应体: {body}")

async_headers

异步属性 - 获取响应头

python
headers = await at.response.async_headers

返回值: 响应头字典

使用场景:

  • 获取响应头信息
  • 提取 Token、Cookie 等认证信息
  • 检查响应头中的特定字段

示例:

python
# 获取响应头
headers = await at.response.async_headers
print(f"响应头: {headers}")

# 提取特定响应头
content_type = headers.get("Content-Type")
print(f"Content-Type: {content_type}")

async_code

异步属性 - 获取响应状态码

python
code = await at.response.async_code

返回值: HTTP 状态码(整数)

使用场景:

  • 检查请求是否成功
  • 根据状态码执行不同的逻辑
  • 状态码断言

示例:

python
# 获取状态码
code = await at.response.async_code
print(f"状态码: {code}")

# 根据状态码执行不同逻辑
if code == 200:
    print("请求成功")
elif code == 401:
    print("未授权")
elif code == 500:
    print("服务器错误")

async_time

异步属性 - 获取响应时间(毫秒)

python
time = await at.response.async_time

返回值: 响应时间(毫秒,浮点数)

使用场景:

  • 性能测试
  • 响应时间断言
  • 监控接口性能

示例:

python
# 获取响应时间
time = await at.response.async_time
print(f"响应时间: {time}ms")

# 响应时间断言
assert time < 1000, f"响应时间过长: {time}ms"

async_error

异步属性 - 获取错误信息

python
error = await at.response.async_error

返回值: 错误信息(字典或 None)

使用场景:

  • 请求失败时获取错误详情
  • 错误处理和日志记录

示例:

python
# 获取错误信息
error = await at.response.async_error
if error:
    print(f"请求失败: {error}")

响应处理方法

get_response()

异步方法 - 获取完整响应对象

python
response = await at.response.get_response()

返回值:

  • 成功时:返回响应对象本身(包含 body、headers、code、time)
  • 失败时:返回错误信息字典

使用场景:

  • 一次性获取完整响应信息
  • 判断请求是否成功

示例:

python
# 获取完整响应
response = await at.response.get_response()

# 检查是否有错误
if at.response.is_error:
    print(f"请求失败: {response}")
else:
    print(f"请求成功")
    print(f"状态码: {await at.response.async_code}")
    print(f"响应体: {await at.response.async_body}")

async_json()

异步方法 - 将响应体解析为 JSON

python
json_data = await at.response.async_json()

返回值:

  • 成功时:返回解析后的 JSON 对象(字典或列表)
  • 失败时:返回 None

使用场景:

  • 解析 JSON 格式的响应体
  • 提取响应中的特定字段
  • 数据断言和验证

示例:

python
# 解析 JSON 响应
json_data = await at.response.async_json()
if json_data:
    print(f"JSON 数据: {json_data}")

    # 提取字段
    user_id = json_data.get("user_id")
    username = json_data.get("username")

    # 保存到临时变量
    at.temp.set("user_id", user_id)
    at.temp.set("username", username)
else:
    print("响应体不是有效的 JSON 格式")

async_text()

异步方法 - 将响应体转换为文本

python
text = await at.response.async_text()

返回值: 响应体的字符串表示

使用场景:

  • 获取文本格式的响应
  • 处理非 JSON 格式的响应

示例:

python
# 获取文本响应
text = await at.response.async_text()
print(f"响应文本: {text}")

完整使用示例

接口后置操作:提取响应数据

python
# 获取响应状态码
code = await at.response.async_code
print(f"状态码: {code}")

# 解析 JSON 响应
json_data = await at.response.async_json()
if json_data:
    # 提取用户信息
    user_id = json_data.get("data", {}).get("user_id")
    token = json_data.get("data", {}).get("token")

    # 保存到临时变量供后续接口使用
    at.temp.set("user_id", user_id)
    at.temp.set("access_token", token)

    print(f"用户 ID: {user_id}")
    print(f"Token: {token}")

接口后置操作:响应断言

python
# 获取响应状态码
code = await at.response.async_code
assert code == 200, f"期望状态码 200,实际 {code}"

# 获取响应时间
time = await at.response.async_time
assert time < 1000, f"响应时间过长: {time}ms"

# 解析 JSON 并断言
json_data = await at.response.async_json()
assert json_data is not None, "响应体不是有效的 JSON"
assert json_data.get("code") == 0, f"业务错误码: {json_data.get('code')}"
assert json_data.get("message") == "success", f"业务消息异常: {json_data.get('message')}"

print("所有断言通过")

接口后置操作:提取响应头信息

python
# 获取响应头
headers = await at.response.async_headers
print(f"响应头: {headers}")

# 提取 Token
token = headers.get("Authorization") or headers.get("X-Auth-Token")
if token:
    at.temp.set("auth_token", token)
    print(f"提取到 Token: {token}")

# 提取 Cookie
set_cookie = headers.get("Set-Cookie")
if set_cookie:
    at.temp.set("session_cookie", set_cookie)
    print(f"提取到 Cookie: {set_cookie}")

接口后置操作:错误处理

python
# 获取完整响应
response = await at.response.get_response()

# 检查是否有错误
if at.response.is_error:
    error = await at.response.async_error
    print(f"请求失败: {error}")
    # 可以选择抛出异常或记录日志
    raise RuntimeError(f"接口请求失败: {error}")
else:
    # 正常处理响应
    code = await at.response.async_code
    body = await at.response.async_body
    print(f"请求成功 - 状态码: {code}, 响应体: {body}")

接口后置操作:性能监控

python
# 获取响应时间
time = await at.response.async_time
print(f"响应时间: {time}ms")

# 根据响应时间分级
if time < 100:
    level = "优秀"
elif time < 500:
    level = "良好"
elif time < 1000:
    level = "一般"
else:
    level = "较差"

print(f"性能等级: {level}")

# 保存到临时变量
at.temp.set("response_time", str(time))
at.temp.set("performance_level", level)

接口后置操作:链式数据提取

python
# 解析 JSON 响应
json_data = await at.response.async_json()

if json_data:
    # 提取嵌套数据
    data = json_data.get("data", {})
    user_info = data.get("user_info", {})

    # 提取多个字段
    user_id = user_info.get("id")
    username = user_info.get("username")
    email = user_info.get("email")
    phone = user_info.get("phone")

    # 批量保存到临时变量
    at.temp.set("user_id", str(user_id))
    at.temp.set("username", username)
    at.temp.set("email", email)
    at.temp.set("phone", phone)

    print(f"提取用户信息: ID={user_id}, 用户名={username}")

注意事项

  • 异步调用:所有属性和方法都必须使用 await 调用
  • 属性访问:异步属性不需要括号(如 await at.response.async_body
  • 方法调用:异步方法需要括号(如 await at.response.async_json()
  • 错误检查:可以通过 at.response.is_error 判断请求是否失败
  • JSON 解析async_json() 解析失败时返回 None,需要进行判空检查
  • 响应时间async_time 返回的是毫秒数(浮点数)
  • 使用场景:响应对象通常在接口后置操作中使用

总结

响应对象提供了完整的响应访问能力:

  • 通过 at.response 访问响应对象
  • 支持获取响应体、响应头、状态码、响应时间、错误信息
  • 提供 JSON 解析和文本转换方法
  • 所有属性和方法都是异步的,需要使用 await 调用
  • 属性访问无括号,方法调用有括号
  • 适合在接口后置操作中进行数据提取、断言验证、性能监控