apifox-test-case
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese接口测试用例
API Test Cases
前置条件:先阅读。若旧总入口与本 skill 的领域规则冲突,以当前 CLI help 和本 skill 为准。涉及接口定义时按当前 CLI help 使用 endpoint、schema、folder 等命令;涉及多步骤流程时读取../apifox-cli/SKILL.md。../apifox-test-scenario/SKILL.md
具体命令参数以当前 CLI help 为准。创建和更新接口测试用例时重点处理 categoryId 展示风险、test-case 与 test-scenario 边界、requestBody/processor/assertion/extractor 结构和运行验证边界。Agent 写入前必须用 校验 payload,写入后用 回读确认保存结构。
cli-schema validategetPrerequisites: Readfirst. If there is a conflict between the old main entry and the domain rules of this skill, follow the current CLI help and this skill. When involving interface definitions, use commands like endpoint, schema, folder according to the current CLI help; when involving multi-step processes, read../apifox-cli/SKILL.md.../apifox-test-scenario/SKILL.md
Refer to the current CLI help for specific command parameters. When creating and updating API test cases, focus on handling risks related to categoryId display, boundaries between test-case and test-scenario, structures of requestBody/processor/assertion/extractor, and boundaries of operation verification. The Agent must use to verify the payload before writing, and use to read back and confirm the saved structure after writing.
cli-schema validateget何时使用
When to Use
- 创建接口下的自动化测试用例。
- 更新测试步骤、断言、提取变量、前后置处理器。
- 查看某个 endpoint 下有哪些测试用例。
- 按 caseId、endpointId、categoryId 运行接口测试用例。
- 创建或维护测试数据集。
- 排查“测试步骤无法展示”“断言不生效”“提取变量为空”。
- Create automated test cases under an API.
- Update test steps, assertions, variable extraction, pre/post processors.
- Check which test cases exist under a specific endpoint.
- Run API test cases by caseId, endpointId, or categoryId.
- Create or maintain test datasets.
- Troubleshoot issues like 'test steps not displaying', 'assertions not taking effect', 'variable extraction returns empty'.
不应使用
When Not to Use
- 多接口流程编排或测试场景建模:转 。
apifox-test-scenario - 只运行已有场景、套件或 CI 命令:转 。
apifox-test-automation - 只改接口定义、请求参数或响应模型:按当前 CLI help 使用 endpoint/schema 等 API 设计命令。
- 只查看测试报告:按当前 CLI help 使用 ;执行和报告边界参考
test-report。apifox-test-automation
- Multi-API process orchestration or test scenario modeling: Switch to .
apifox-test-scenario - Only run existing scenarios, suites or CI commands: Switch to .
apifox-test-automation - Only modify API definitions, request parameters or response models: Use API design commands like endpoint/schema according to the current CLI help.
- Only view test reports: Use according to the current CLI help; refer to
test-reportfor boundaries between execution and reports.apifox-test-automation
核心概念
Core Concepts
| 概念 | CLI 资源 | 说明 |
|---|---|---|
| 接口测试用例 | | 绑定接口 endpoint 的测试数据与步骤 |
| 测试分类 | | 测试用例分类;创建 case 前用于获取有效 |
| 测试数据集 | | 可供迭代运行的数据 |
| 接口定义 | | case 的依赖对象,不等同 case 本身 |
| 测试场景 | | 多步骤流程编排,边界不同 |
| Concept | CLI Resource | Description |
|---|---|---|
| API Test Case | | Test data and steps bound to an API endpoint |
| Test Category | | Test case category; used to obtain valid |
| Test Dataset | | Data available for iterative runs |
| API Definition | | Dependent object of a case, not equivalent to the case itself |
| Test Scenario | | Multi-step process orchestration with different boundaries |
命令入口
Command Entry Points
使用当前 CLI help 查询 、 和 的参数。 用于获取 ;当前 不支持 。按接口查看已有用例时使用 ; 只接收 caseId。
test-casetest-dataapifox run --test-casetest-case categorycategoryIdtest-case category--endpointtest-case list --endpoint <endpointId>apifox run --test-case把单接口用例导入测试场景时,不在本 skill 手写场景步骤;转 并使用:
apifox-test-scenariobash
apifox test-scenario import-steps <scenarioId> --project <projectId> --source test-case --endpoint <endpointId> --ids <testCaseIds> --sync manualUse the current CLI help to query parameters for , and . is used to obtain ; the current does not support . To view existing cases under an API, use ; only accepts caseId.
test-casetest-dataapifox run --test-casetest-case categorycategoryIdtest-case category--endpointtest-case list --endpoint <endpointId>apifox run --test-caseWhen importing single-API cases into test scenarios, do not manually write scenario steps in this skill; switch to and use:
apifox-test-scenariobash
apifox test-scenario import-steps <scenarioId> --project <projectId> --source test-case --endpoint <endpointId> --ids <testCaseIds> --sync manual创建测试用例标准流程
Standard Process for Creating Test Cases
- 确认项目和分支。
- 定位 endpoint:。
apifox endpoint list/get - 必须执行 获取有效
apifox test-case category --project <projectId>;如需查看某接口下已有用例,执行categoryId。apifox test-case list --project <projectId> --endpoint <endpointId> - 如果已有类似 case,先 ,再
apifox test-case list --project <projectId> --endpoint <endpointId>一个作为模板。get - 获取并校验 schema。
test-case-create - 构造完整 JSON,不要只写空壳 name/endpointId。
- 创建后立即 ,确认后端实际保存结构。
test-case get <caseId> - 运行一次并生成本地 JSON 报告,确认 requestBody、处理器、断言和脚本在 runner 中实际生效。
- 如上传报告,按当前 CLI help 使用 检查步骤详情;若报告缺详情,参考
test-report的本地/云端报告边界。apifox-test-automation
categoryIdcategoryIdget/listtest-case category- Confirm the project and branch.
- Locate the endpoint: .
apifox endpoint list/get - Must execute to obtain a valid
apifox test-case category --project <projectId>; if you need to view existing cases under an API, executecategoryId.apifox test-case list --project <projectId> --endpoint <endpointId> - If similar cases exist, first run , then
apifox test-case list --project <projectId> --endpoint <endpointId>one as a template.get - Obtain and verify the schema.
test-case-create - Construct a complete JSON, do not only write empty name/endpointId.
- Immediately run after creation to confirm the actual saved structure on the backend.
test-case get <caseId> - Run once and generate a local JSON report to confirm that requestBody, processors, assertions and scripts actually take effect in the runner.
- If uploading the report, use according to the current CLI help to check step details; if the report lacks details, refer to the local/cloud report boundaries in
test-report.apifox-test-automation
categoryIdcategoryIdget/listtest-case category更新测试用例标准流程
Standard Process for Updating Test Cases
更新时必须先 原结构并基于完整结构修改,再校验 schema,避免丢失已有步骤、断言、变量提取或处理器。 不是 JSON Patch,也不会按 id 合并数组元素。
gettest-case-updateupdatetest-case-createtest-case-updatetest-case-updatetypedataextractorvariableType/subject/shareScopeassertionsubject/comparisondelaydatatest-case getmethodtest-case-updateWhen updating, must first the original structure and modify based on the complete structure, then verify the schema to avoid losing existing steps, assertions, variable extraction or processors. is not a JSON Patch, nor will it merge array elements by id.
gettest-case-updateupdateThe and schemas already include descriptions of pre/post processors, assertions, variable extraction and enumeration values. When writing a processor for the first time, check the schema first, do not guess field names, enumeration values or old formats based on experience. The current will verify the structure according to the processor ; for example, will verify , will verify , and the of must be a number.
test-case-createtest-case-updatetest-case-updatedatatypeextractorvariableType/subject/shareScopeassertionsubject/comparisondatadelayThe in the structure read back by may be an empty string; this does not indicate an abnormal method display on the frontend, as the client usually displays the HTTP method from the bound endpoint. The current schema is compatible with this read-back structure, do not reverse-guess the value to fill the method during update unless the user explicitly wants to modify the bound API or request method.
methodtest-case gettest-case-update测试步骤展示规则
Display Rules for Test Steps
如果用户关心前端展示,必须额外验证:
- 创建或更新后执行 。
test-case get - 确认步骤、断言、提取变量、处理器字段被后端保存。
- 确认字段不是空数组、空对象或写到错误层级。
- 如果 CLI 返回成功但前端不展示,转 ,先确认 project、branch、endpoint、categoryId 和回读结构是否一致。
apifox-cli-checkup
If the user cares about frontend display, additional verification must be performed:
- Execute after creation or update.
test-case get - Confirm that steps, assertions, variable extraction, and processor fields are saved by the backend.
- Confirm that the fields are not empty arrays, empty objects, or written to the wrong level.
- If the CLI returns success but the frontend does not display, switch to and first confirm whether the project, branch, endpoint, categoryId and read-back structure are consistent.
apifox-cli-checkup
内容和处理器结构
Content and Processor Structures
- 必须是字符串,不要把 JSON Body 写成对象。
requestBody.data - 多行 JSON Body、前置脚本和后置脚本用 预格式化;这只影响客户端展示可读性,不改变执行语义。
\n - 、
preProcessors使用扁平结构postProcessors,不要写旧式嵌套{ id, type, data, defaultEnable, enable }。{ type, config } - 处理器建议带稳定 ,尤其是
id、assertion、extractor;缺少customScript可能 validate 通过但运行器或客户端解析异常。id - 提取全局变量时,使用
data.variableType;如需指定生效范围,globals优先使用data.shareScope。PROJECT是团队范围,可能依赖增值能力,除非用户明确要求团队范围,否则不要默认使用。TEAM - 写入前照常跑 ,写入后
cli-schema validate回读确认保存结构。test-case get
示例:
json
{
"requestBody": {
"type": "application/json",
"data": "{\n \"name\": \"Demo\",\n \"description\": \"Readable in client\"\n}"
},
"postProcessors": [
{
"id": "postProcessors.0.customScript",
"type": "customScript",
"data": "pm.test('返回 ID', function () {\n var body = pm.response.json();\n pm.expect(body.data.id).to.exist;\n});",
"defaultEnable": true,
"enable": true
},
{
"id": "postProcessors.1.extractor",
"type": "extractor",
"data": {
"variableName": "project_pet_name",
"variableType": "globals",
"shareScope": "PROJECT",
"subject": "responseJson",
"expression": "$.name"
},
"defaultEnable": true,
"enable": true
}
]
}- must be a string, do not write JSON Body as an object.
requestBody.data - Multi-line JSON Body, pre-scripts and post-scripts are pre-formatted with ; this only affects readability in the client, not execution semantics.
\n - and
preProcessorsuse a flat structurepostProcessors, do not use the old nested structure{ id, type, data, defaultEnable, enable }.{ type, config } - It is recommended to include a stable for processors, especially
id,assertion,extractor; missingcustomScriptmay pass validation but cause parsing exceptions in the runner or client.id - When extracting global variables, use for
globals; if you need to specify the effective scope, preferdata.variableTypeforPROJECT.data.shareScopeis a team-wide scope and may depend on value-added capabilities, do not use it by default unless the user explicitly requests team-wide scope.TEAM - Run as usual before writing, and use
cli-schema validateto read back and confirm the saved structure after writing.test-case get
Example:
json
{
"requestBody": {
"type": "application/json",
"data": "{\n \"name\": \"Demo\",\n \"description\": \"Readable in client\"\n}"
},
"postProcessors": [
{
"id": "postProcessors.0.customScript",
"type": "customScript",
"data": "pm.test('返回 ID', function () {\n var body = pm.response.json();\n pm.expect(body.data.id).to.exist;\n});",
"defaultEnable": true,
"enable": true
},
{
"id": "postProcessors.1.extractor",
"type": "extractor",
"data": {
"variableName": "project_pet_name",
"variableType": "globals",
"shareScope": "PROJECT",
"subject": "responseJson",
"expression": "$.name"
},
"defaultEnable": true,
"enable": true
}
]
}断言和脚本规则
Assertion and Script Rules
常规校验优先用可视化 ,自定义脚本只作为兜底能力。
assertion断言规则:
- HTTP 状态码、JSON 字段存在/相等、文本包含等常规校验优先用可视化 ,不要默认写
assertion。customScript - 可视化断言字段使用当前 schema 枚举:HTTP 状态码用 ,不要用
httpCode;JSON 字段用responseCode,不要用responseJson;全文包含用responseBody+responseText;比较符用include,不要用equal。equals
json
{
"type": "assertion",
"data": {
"name": "HTTP 状态码为 200",
"subject": "httpCode",
"comparison": "equal",
"value": "200",
"path": ""
},
"defaultEnable": true,
"enable": true
}脚本规则:
- 自定义脚本只用于可视化断言覆盖不了的逻辑,例如复杂数组筛选、条件判断、二次请求、XML 转换或 schema 校验。
- Apifox 脚本运行时通过 对象读写变量、访问响应和定义断言;脚本断言使用
pm包裹。pm.test(...) - 不要在 外裸调用
pm.test,避免空响应或非 JSON 响应导致错误难定位。pm.response.json() - 不要凭经验扩展运行上下文字段。
js
pm.environment.set("variable_key", "variable_value");
pm.variables.set("variable_key", "variable_value");
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("JSON value equals expected", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});Prioritize using visual for regular checks, and only use custom scripts as a fallback.
assertionAssertion Rules:
- For regular checks such as HTTP status code, JSON field existence/equality, text inclusion, prioritize using visual , do not default to writing
assertion.customScript - Use current schema enumerations for visual assertion fields: use for HTTP status codes, not
httpCode; useresponseCodefor JSON fields, notresponseJson; useresponseBody+responseTextfor full-text inclusion; useincludefor comparison operators, notequal.equals
json
{
"type": "assertion",
"data": {
"name": "HTTP 状态码为 200",
"subject": "httpCode",
"comparison": "equal",
"value": "200",
"path": ""
},
"defaultEnable": true,
"enable": true
}Script Rules:
- Custom scripts are only used for logic that cannot be covered by visual assertions, such as complex array filtering, conditional judgment, secondary requests, XML conversion or schema validation.
- Apifox scripts use the object to read/write variables, access responses and define assertions during runtime; script assertions are wrapped with
pm.pm.test(...) - Do not call outside
pm.response.json()to avoid difficult-to-locate errors caused by empty responses or non-JSON responses.pm.test - Do not extend runtime context fields based on experience.
js
pm.environment.set("variable_key", "variable_value");
pm.variables.set("variable_key", "variable_value");
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("JSON value equals expected", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});字段风险提醒
Field Risk Reminders
- 不要把 的步骤结构写进
test-scenario。test-case - 不要只写 case 名称和 endpointId,这会形成前端看起来没有步骤的空壳。
- 不要使用未验证的 。
categoryId - 不要仅凭 返回的
test-case get为空判断前端展示异常;客户端可能从绑定 endpoint 展示 HTTP method,methodschema 也兼容该回读值。test-case-update - 不要凭经验猜 processor/assertion/extractor 的字段名。
- 不要用 endpoint response 代替 test-case assertion。
- 不要把单接口 test-case 写成多步骤场景;test-case 不支持场景步骤间数据传递。
- 不要把场景里的 、
{{$.1.response...}}等步骤间传递规则写进 test-case;如果需要跨步骤流程,创建或维护forEach。test-scenario - 如果用户要把已有 test-case 作为场景步骤复用,使用 ,导入后再
test-scenario import-steps --source test-case回读并补业务参数值。get --with-case-detail
- Do not write the step structure of into
test-scenario.test-case - Do not only write the case name and endpointId, which will form an empty shell with no steps visible on the frontend.
- Do not use unverified .
categoryId - Do not judge frontend display exceptions solely based on the empty returned by
method; the client may display the HTTP method from the bound endpoint, and thetest-case getschema is also compatible with this read-back value.test-case-update - Do not guess field names of processor/assertion/extractor based on experience.
- Do not use endpoint response instead of test-case assertion.
- Do not write single-API test-case as a multi-step scenario; test-case does not support data transfer between scenario steps.
- Do not write inter-step transfer rules like or
{{$.1.response...}}from scenarios into test-case; if cross-step processes are needed, create or maintainforEach.test-scenario - If the user wants to reuse existing test-case as scenario steps, use , then read back with
test-scenario import-steps --source test-caseand fill in business parameter values after import.get --with-case-detail
运行规则
Operation Rules
- 是按单个 case 运行。
test-case run <caseId> - 是按接口运行该接口下可运行 case。
test-case run --endpoint <endpointId> - 必须和
--category <categoryId>一起使用。--endpoint <endpointId> - 只支持 caseId,不支持 endpoint/category selector。
apifox run --test-case <caseId> - 可省略,但为了复现建议显式指定。
--environment - 创建或更新后至少运行一次,确认 requestBody、处理器、断言和脚本在 runner 中实际生效;和
cli-schema validate成功不等于运行期一定正确。test-case get
- runs a single case.
test-case run <caseId> - runs runnable cases under the specified API.
test-case run --endpoint <endpointId> - must be used together with
--category <categoryId>.--endpoint <endpointId> - only supports caseId, not endpoint/category selector.
apifox run --test-case <caseId> - can be omitted, but it is recommended to specify it explicitly for reproducibility.
--environment - Run at least once after creation or update to confirm that requestBody, processors, assertions and scripts actually take effect in the runner; success in and
cli-schema validatedoes not guarantee correctness during runtime.test-case get
常见恢复
Common Troubleshooting
| 现象 | 处理 |
|---|---|
| 测试步骤不展示 | |
| 断言不生效 | 读取现有成功 case 模板,对比 assertion 字段 |
| 提取变量为空 | 检查 extractor 层级、变量名、响应路径和执行报告 |
| run-config 404 | 确认 case/endpoint/environment/branch 均存在,再转 |
| endpoint 下找不到 case | 检查是否带了正确 |
| Phenomenon | Handling |
|---|---|
| Test steps not displaying | Check the actual structure with |
| Assertions not taking effect | Read existing successful case templates and compare assertion fields |
| Variable extraction returns empty | Check extractor level, variable name, response path and execution report |
| run-config 404 | Confirm that case/endpoint/environment/branch all exist, then switch to |
| Cannot find case under endpoint | Check if the correct |