# 9.4 自动化研发流水线与 QA

软件测试与流水线一直是劳动密集型工作。从编写测试用例，到维护自动化脚本，再到手动做 UI 点检。 Claude 的出现，特别是其 **Vision** 和 **Code** 能力的结合，让 QA 行业迎来了变革。

## 9.4.1 单元测试自动生成

即使团队没有时间写测试，Claude 也可以作为“低成本的测试补充”。

### 场景：为遗留代码补充测试

**Input Code (Legacy Java)**:

```java
public double calculateShipping(Order order) {
    if (order.getTotal() > 100) return 0;
    if (order.getWeight() > 10) return 20;
    return 10;
}
```

**Prompt**:

> “请为这个方法编写 JUnit 5 测试用例。请确保覆盖所有逻辑分支（100% Branch Coverage），特别是边界条件。”

**Claude Output (JUnit 5)**:

```java
@Test
void testFreeShipping() {
    Order order = new Order(101.0, 5.0); // > 100
    assertEquals(0, calculator.calculateShipping(order));
}

@Test
void testHeavyShipping() {
    Order order = new Order(50.0, 11.0); // <= 100, > 10
    assertEquals(20, calculator.calculateShipping(order));
}

@Test
void testStandardShipping() {
    Order order = new Order(50.0, 9.0); // <= 100, <= 10
    assertEquals(10, calculator.calculateShipping(order));
}

@Test
void testBoundary() {
    Order order = new Order(100.0, 10.0); // 边界值
    assertEquals(10, calculator.calculateShipping(order));
}
```

Claude 精准地识别了 `100` 和 `10` 这两个边界值，并生成了对应的 Assert。

## 9.4.2 UI 视觉回归测试

这是 Claude Vision 的杀手锏。相比于传统的像素级对比（Pixel Diff），Claude 能够进行 **语义化断言 (Semantic Assertion)**。

### 场景：响应式布局检查

**Prompt with Image**:

> “这是我们在 iPhone 12 Mini 上的网页截图。请检查：
>
> 1. ‘立即购买’按钮是否被底部导航栏遮挡？
> 2. 产品标题是否发生了异常换行？”

**Claude Response**:

> **FAIL**:
>
> 1. ‘立即购买’按钮的下半部分（约 50%）被 TabBar 遮挡，用户难以点击。建议增加 `padding-bottom`。
> 2. 标题显示正常，没有异常换行。

**代码集成 (Playwright + AI SDK)**:

```javascript
test("Check critical layout deterministically", async ({ page }) => {
  await page.goto("/product/123");

  // 硬门禁优先使用 DOM、可访问性和截图 diff 等确定性断言
  const buyButton = page.getByRole("button", { name: "立即购买" });
  await expect(buyButton).toBeVisible();
  await expect(buyButton).toBeInViewport();
});
```

Claude 更适合做语义 triage：解释截图 diff、归类视觉失败原因、生成修复建议。若必须让模型参与自动判断，应要求结构化 JSON、固定 rubric、模型版本、重试策略、人工复核队列和 flake 处理，而不是直接把非确定性 LLM judge 当成唯一 CI 硬断言。

## 9.4.3 根本原因分析

当 CI 挂了，通常会甩给你几千行的日志。 **Log Analysis Agent** 可以大大缩短 MTTR (平均修复时间)。

**Agent Workflow**:

1. **Input**: 粘贴 Jenkins 构建失败的 2000 行日志。
2. **Filter**: Agent 自动忽略 Info/Warn，锁定 `Caused by: java.lang.NoSuchMethodError`。
3. **Correlate**: Agent 结合 Git API，查看最近 24 小时的提交记录。
4. **Conclusion**:

   > “构建失败的原因是 `guava` 库依赖冲突。 提交 `feat: update dependencies` (hash: a1b2c3) 将 Guava 从 28.0 升级到了 30.0，删除了 `DirectExecutor` 方法。 **建议修复**: 回滚该提交，或修改代码使用新的 Executor API。”

## 9.4.4 探索性测试

这是 **Computer Use** 的最佳应用场景。 给 Claude 一个新上线的 App（安装包或测试网址），给它一个任务：

> “你是一个挑剔的用户。请试用我们的购物车流程。尝试修改商品数量为负数、超大数、或特殊字符，看看系统是否会崩溃。如果崩溃，请截图报告。”

Claude 会自主操作浏览器，点击输入框，输入 `-100`，点击结算。如果弹出了一个未捕获的异常堆栈页面，它会立即截图并报警：“发现严重 Bug，数量允许输入负数导致后端 500 错误。”

## 9.4.5 CI/CD 流水线与代码自动化审查

随着生态的延展，AI 除了用于本地研发环节，还能入驻团队核心基础建设如 GitHub Actions, GitLab CI/CD 环境。

**典型场景：**

1. **自动 PR 审查与 Review**：开发提交 Pull Request 后，流水线唤醒底层使用 Claude Code 引擎搭建的自动化工作流。它可以直接针对改动的文件做大批量的代码质量甚至安全薄弱面进行多维度的预检查。
2. **Issue 分流器与智能工单分配**：对企业内部海量的用户填报工单，Claude 结合其超大上下文能力可以将各类 BUG 通过特征提取归类，自动打入相应标签，甚至是为开发提供详尽的代码排查意见与最小可复现模型。

***

至此，已经看完了从客服、文档、数据到测试流水线的四大核心场景。 这些案例证明了：**AI 不是玩具，它是生产力。** 但随着 AI 应用规模的扩大，面临着新的问题：**贵**。Token 很贵，延迟很高。如何让 AI 又快又省？

➡️ [第十章：成本优化与性能调优](/claude_guide/di-si-bu-fen-shi-zhan-pian/10_optimization.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yeasy.gitbook.io/claude_guide/di-si-bu-fen-shi-zhan-pian/09_practical/9.4_qa_test.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
