惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 小海海宁宁

从0到1:使用Schemathesis搭建API自动化测试框架实战 OpenAPI规范及自动化用例生成 playwright & selenium对Shadow DOM的处理 自动化平台质量体系建设(草稿) 【模板】邮件提测模板 研发测试质量规范(草稿) 自动化框架 - Q&A 自动化框架 - 核心模块使用指南 - Kafka 自动化框架 - 核心模块使用指南 - Kibana 自动化框架 - 核心模块使用指南 - Playwright 自动化框架 - 核心功能使用指南 - 动态值生成 自动化框架 - 核心功能使用指南 - 数据处理模块 自动化框架 - 核心功能使用指南 - 数据校验模块 自动化框架 - 核心功能使用指南 - 上下文值传递 自动化框架 - 核心功能使用指南 - 用例过滤Tag使用 自动化框架 - 核心功能使用指南 - Excel测试用例编写 自动化框架 - 核心功能使用指南 - 配置文件使用指南 自动化框架使用说明 - 快速开始 Kibana如何添加可视化看板
自动化框架 - 核心模块使用指南 - API
小海海宁宁 · 2026-03-09 · via 博客园 - 小海海宁宁
  1. 模块介绍

APIModule 是一个用于发送 HTTP 请求和管理 API 调用的模块。它支持从配置初始化、参数注入、上下文管理、请求重试等功能。以下是如何使用 APIModule 的详细指南。

  1. 初始化模块

初始化 APIModule,可以通过传递配置字典来初始化模块,配置字典中包含基础 URL、超时时间、重试次数等信息

api_module:
  class_path: "src.modules.api.api.APIModule"
  base_url: "http://10.245.218.237:8808/"
  timeout: 30
  retry_attempts: 3
  retry_delay: 1

参数说明:

class_path: 模块对应的类路径。只能为: src.modules.api.api.APIModule base_url: 模块的基础URL。例如,http://10.245.209.149:81/。 timeout: 请求超时时间(秒),可选。例如,30。 retry_attempts: 重试次数,可选。例如,3。 retry_delay: 重试间隔时间(秒),可选。例如,1.

  1. 核心方法

Action 配置为 ”send_request”,用于发送请求和接收响应。

参数说明。

!!!注意:以下参数必须通过ParamsSetID 传入,即参数必须写在测试数据表中。

  • API: str

    • 描述: endpoint,请求的终点 URL。可以是相对路径或完整 URL。
    • 必填
    • 示例: "/posts" 或 "https://jsonplaceholder.typicode.com/posts"
  • method: str = 'GET'

    • 描述: HTTP 请求方法,如 GET, POST, PUT, DELETE 等。
    • 默认值: 'GET'
    • 示例: "POST"
  • body: Optional[Dict[str, Any]] = None

    • 描述: 请求体,通常用于 POST, PUT 方法。
    • 默认值: None
    • 示例: {"title": "foo", "body": "bar", "userId": 1}
  • header: Optional[Dict[str, str]] = None

    • 描述: 请求头,可以覆盖默认的请求头。
    • 默认值: None
    • 示例: {"Authorization": "Bearer your_token"}
  • saveAsVariable:str,用于存储返回值

    • 返回 requests 库的 Response 对象,包含请求的响应信息
    • 必填
    • 示例:responseResult
  1. Excel中的使用

在测试用例表中:

Module:使用的模块名称,这里是 oah_api, 在yml中已配置。

Action:模块中的具体操作, send_request是API模块的核心方法。

Tag:可选的标签,用于分类或标识。

Description:描述当前操作,例如 validate 200 表示校验状态码为 2

ParamsSetID: 参数将从测试数据表获取

Description Module Action Tag Params ParamsSetID
basicConfig edit oah_api send_request     oah_request

在测试数据表中, 参数与send_request 要求的参数一一对应,请看核心方法的参数描述

ParamsSetID ParamID Name Value DataGroup
oah_request P1 API /au/api/ops/oah/tenant/info/basicConfig/edit 1
oah_request P2 method POST 1
oah_request P3 body @body.string:value 1
oah_request P4 header {
"Content-Type": "application/json",
"oah-app-id": "12323",
"oah-sign": "@sign.string:value",
"oah-sign-type": "ASK",
"oah-timestamp": "1761113092294"
}
1
oah_request P5 saveAsVariable response 1

这5个参数也可以写到一个表格中, Name需要定义为params

ParamsSetID ParamID Name Value DataGroup TestCaseID
租户参数 P1 params {
"API": "au/api/ops/tenant/info/create",
"header": {
"Content-Type": "application/json",
"Authorization": "@Atoken.string:value"
},
"body": {
"country": "CN",
"timeZone": "Asia/Shanghai",
"domain": "swjAPIT%random_string:4%",
"tenantName": "framework_admin_auto_tenant%random_string:4%",
"expiredTime": "2029-07-30 23:59:59",
"adminAccount": "framework_admin_auto_%date%_b_%random_string:4%",
"adminPassword": "Qa_119576",
"confirmPassword": "Qa_119576"
},
"method": "POST",
"saveAsVariable": "response"
}
1 TC_administrator_tenant_create

如果该用例只有一组测试数据,那么params可以选择写在测试数据表,或在测试用例表

TestCaseID LFCaseID ScenarioDescription Step Description Module Action Tag Params
TC_administrator_tenant_create   新建租户 1 新增租户 admin_api send_request regression,prepare {
"API": "au/api/ops/tenant/info/create",
"header": {
"Content-Type": "application/json",
"Authorization": "@Atoken.string:value"
},
"body": {
"country": "CN",
"timeZone": "Asia/Shanghai",
"domain": "swjAPIT%random_string:4%",
"tenantName": "framework_admin_auto_tenant%random_string:4%",
"expiredTime": "2029-07-30 23:59:59",
"adminAccount": "framework_admin_auto_%date%_b_%random_string:4%",
"adminPassword": "Qa_119576",
"confirmPassword": "Qa_119576"
},
"method": "POST",
"saveAsVariable": "response"
}
      2 校验返回码200 value_validation validate_data   {
"field_name": "status_code",
"actual_value": "@response.json:data.code",
"operator": "equal",
"expect_value": "200"
}
      3 校验返回消息操作成功 value_validation validate_data   {
"field_name": "msg",
"actual_value": "@response.json:data.msg",
"operator": "equal",
"expect_value": "操作成功"
}