营销与销售· 测试方案和测试用例

按代码变更安排测试顺序

结合代码变更、测试覆盖范围和影响程度,为持续集成或限时回归排出测试顺序,并说明排序依据。

# 测试套件优先级排序器

根据代码变更和项目影响,智能安排测试执行的优先级。

## 核心能力

此技能通过以下方式优化测试执行:

1. **分析代码变更** - 确定哪些文件被修改
2. **映射测试覆盖范围** - 确定哪些测试覆盖了变更的代码
3. **计算优先级分数** - 按影响和相关性排列测试
4. **生成执行顺序** - 创建优化后的测试运行序列
5. **提供理由** - 解释每项测试优先级的确定原因

## 测试优先级排序流程

### 第 1 步:确定代码变更

确定哪些代码已被修改,以便集中测试工作。

**变更信息来源:**
- Git diff(最近一次提交或未提交的更改)
- 拉取请求的文件列表
- 用户提及的特定文件
- 工作目录中修改过的文件

**收集变更文件:**

```bash
# Recent commit changes
git diff --name-only HEAD~1 HEAD

# Uncommitted changes
git diff --name-only

# Changes in current branch vs main
git diff --name-only main...HEAD

# Files in last N commits
git diff --name-only HEAD~N HEAD
```

**输出示例:**
```
src/auth/login.py
src/payment/processor.py
src/models/user.py
tests/test_auth.py
```

### 第 2 步:分析测试覆盖范围

映射哪些测试覆盖了变更的代码。

**策略:**

**1. 测试与代码的直接映射**
- 与变更代码位于同一目录的测试
- 名称相似的测试(例如,`user.py` → `test_user.py`)
- 导入了变更模块的集成测试

**2. 静态分析**
- 解析测试文件中的导入项
- 确定每项测试使用的模块
- 映射传递依赖关系

**3. 覆盖率数据(如有)**
- 使用 pytest-cov、coverage.py、JaCoCo 的输出
- 确定执行了变更代码行的测试
- 最准确,但需要事先运行测试

**映射示例:**

```
Changed: src/auth/login.py
Covered by:
  - tests/test_auth.py::test_login_success
  - tests/test_auth.py::test_login_failure
  - tests/integration/test_user_flow.py::test_complete_registration
  - tests/e2e/test_authentication_flow.py::test_full_auth_cycle

Changed: src/payment/processor.py
Covered by:
  - tests/test_payment.py::test_process_payment
  - tests/test_payment.py::test_refund
  - tests/integration/test_checkout.py::test_complete_purchase
```

### 第 3 步:计算优先级分数

根据变更影响分配优先级分数。

**评分公式:**

```
Priority Score = Base Score × Change Impact Factor

Base Score factors:
- Direct coverage of changed file: +10 points
- Indirect coverage (imports changed file): +5 points
- Integration test covering changed area: +3 points
- Test in same module: +2 points

Change Impact Factor (multiply):
- Critical path code (auth, payments): ×1.5
- Recently failed test: ×1.3
- High historical failure rate: ×1.2
- Core business logic: ×1.2
- UI/cosmetic changes: ×0.8
```

**评分示例:**

```python
# Example: test_login_success
base_score = 10  # Directly tests login.py (changed)
impact_factor = 1.5  # Auth is critical path
priority_score = 10 × 1.5 = 15

# Example: test_user_flow
base_score = 5 + 3  # Imports login.py + integration test
impact_factor = 1.2  # Core business logic
priority_score = 8 × 1.2 = 9.6

# Example: test_ui_styling
base_score = 2  # Same module as changed file
impact_factor = 0.8  # UI/cosmetic
priority_score = 2 × 0.8 = 1.6
```

### 第 4 步:生成按优先级排列的测试列表

创建包含分数和理由的有序执行序列。

**输出格式:**

```markdown
# Test Execution Priority List

## Summary
- Total tests analyzed: 45
- High priority (score ≥ 10): 8 tests
- Medium priority (score 5-9): 15 tests
- Low priority (score < 5): 22 tests
- Estimated time for high priority tests: ~2 minutes

## Recommended Execution Order

### Priority 1: Critical Tests (Run First)

1. **tests/test_auth.py::test_login_success** (Score: 15.0)
   - Directly tests modified file: src/auth/login.py
   - Critical path: Authentication
   - Reason: Core security functionality changed

2. **tests/test_auth.py::test_login_failure** (Score: 15.0)
   - Directly tests modified file: src/auth/login.py
   - Critical path: Authentication
   - Reason: Error handling for authentication changed

3. **tests/test_payment.py::test_process_payment** (Score: 15.0)
   - Directly tests modified file: src/payment/processor.py
   - Critical path: Payment processing
   - Reason: Payment logic modified, high business impact

### Priority 2: High Impact Tests

4. **tests/integration/test_user_flow.py::test_complete_registration** (Score: 9.6)
   - Indirect coverage: Imports src/auth/login.py
   - Integration test: End-to-end user flow
   - Reason: Validates auth changes in realistic scenario

5. **tests/integration/test_checkout.py::test_complete_purchase** (Score: 9.6)
   - Indirect coverage: Imports src/payment/processor.py
   - Integration test: Full checkout flow
   - Reason: Validates payment changes with real workflow

6. **tests/test_models.py::test_user_model** (Score: 7.0)
   - Related file: src/models/user.py modified
   - Dependency: Auth and payment depend on user model
   - Reason: Foundation for changed functionality

### Priority 3: Supporting Tests

7. **tests/test_validators.py::test_email_validation** (Score: 4.0)
   - Indirect dependency: Used by auth module
   - Reason: Input validation for authentication

8. **tests/e2e/test_authentication_flow.py::test_full_auth_cycle** (Score: 3.9)
   - E2E coverage: Complete authentication flow
   - Reason: Comprehensive validation but slow to execute

[... remaining tests with lower scores ...]

## Quick Commands

Run high priority tests only (8 tests, ~2 min):
```bash
pytest tests/test_auth.py::test_login_success \
       tests/test_auth.py::test_login_failure \
       tests/test_payment.py::test_process_payment \
       tests/integration/test_user_flow.py::test_complete_registration \
       tests/integration/test_checkout.py::test_complete_purchase \
       tests/test_models.py::test_user_model \
       tests/test_validators.py::test_email_validation \
       tests/e2e/test_authentication_flow.py::test_full_auth_cycle
```

仅运行关键测试(3 项测试,约 30 sec):
```bash
pytest tests/test_auth.py tests/test_payment.py::test_process_payment
```

## 时间估算

- 关键测试(3):约 30 seconds
- 高优先级测试(8):约 2 minutes
- 所有已排序测试(23):约 5 minutes
- 完整测试套件(45):约 15 minutes

## 建议

1. **提交前**:提交前运行关键测试(分数 ≥ 15)
2. **CI 快速通道**:在第一阶段运行高优先级测试(分数 ≥ 9)
3. **完整验证**:合并后并行运行所有测试
```

### Step 5: Handle Special Cases

Adjust prioritization for specific scenarios.

**Time-Constrained Testing:**

```markdown
## 1-Minute Quick Check (Top 3 Critical Tests)

pytest tests/test_auth.py::test_login_success \
       tests/test_auth.py::test_login_failure \
       tests/test_payment.py::test_process_payment

These cover the most critical changed functionality.
```

**重构后:**

```markdown
## Post-Refactor Validation

Changed file: src/utils/helpers.py (used by 25 modules)

Priority:
1. All tests directly importing helpers.py (18 tests)
2. Integration tests using helper functions (8 tests)
3. Tests in dependent modules (25 tests)

Reason: Wide-reaching refactor requires comprehensive validation.
```

**不稳定测试处理:**

```markdown
## Note: Flaky Tests Detected

The following tests have high failure rates but low priority scores:
- tests/test_cache.py::test_concurrent_access (30% failure rate)
- tests/test_api.py::test_rate_limiting (25% failure rate)

Recommendation:
- Fix flaky tests before relying on prioritization
- Consider quarantining or mocking timing-dependent code
```

### 第 6 步:提供执行指导

提供实用的命令和策略。

**针对 CI/CD 流水线:**

```yaml
# .github/workflows/ci.yml example
jobs:
  quick-tests:
    name: Critical Tests (Fast Feedback)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Run critical tests
        run: |
          # Run tests with score ≥ 15 first
          pytest -v tests/test_auth.py tests/test_payment.py

  full-tests:
    name: Complete Test Suite
    runs-on: ubuntu-latest
    needs: quick-tests  # Only run if critical tests pass
    steps:
      - uses: actions/checkout@v2
      - name: Run all tests
        run: pytest
```

**针对本地开发:**

```bash
# Pre-commit hook (.git/hooks/pre-commit)
#!/bin/bash

# Get changed files
changed_files=$(git diff --cached --name-only)

# Run prioritized tests for changed files
if echo "$changed_files" | grep -q "src/auth/"; then
    pytest tests/test_auth.py || exit 1
fi

if echo "$changed_files" | grep -q "src/payment/"; then
    pytest tests/test_payment.py || exit 1
fi
```

**针对选择性测试执行:**

```python
# pytest conftest.py - custom marker
import pytest

def pytest_collection_modifyitems(config, items):
    # Mark tests based on priority
    for item in items:
        if "test_auth" in item.nodeid or "test_payment" in item.nodeid:
            item.add_marker(pytest.mark.critical)

# Run only critical tests
# pytest -m critical
```

## 优先级排序策略

### 策略 1:代码变更影响(主要策略)

重点关注覆盖修改代码的测试:

1. **直接覆盖** - 明确测试变更文件的测试
2. **间接覆盖** - 导入或使用变更模块的测试
3. **依赖覆盖** - 针对依赖变更代码的代码所做的测试

**适用时机:**
- 代码修改后
- 提交前验证
- 拉取请求测试

### 策略 2:基于风险的优先级排序

根据业务和技术风险确定优先级:

**高风险领域:**
- 身份认证和授权
- 支付处理
- 数据完整性(数据库操作)
- 安全关键功能
- API 和公共接口

**中风险领域:**
- 业务逻辑
- 用户工作流程
- 数据验证
- 邮件/通知系统

**低风险领域:**
- UI 外观
- 日志记录
- 内部工具
- 开发工具

### 策略 3:失败历史

优先运行经常失败的测试:

```python
# Example: Analyze test failure history from CI
# tests with >10% historical failure rate get boosted priority

Historical Failures:
- test_concurrent_access: 12 failures / 100 runs = 12% → Priority boost ×1.2
- test_database_connection: 5 failures / 100 runs = 5% → No boost
```

### 策略 4:执行时间优化

平衡覆盖范围与速度:

**快速优先策略:**
- 先运行快速单元测试(每项 < 1 second)
- 快速获得基础功能的反馈
- 然后运行较慢的集成/E2E 测试

**关键测试优先策略:**
- 无论速度如何,先运行最重要的测试
- 更适合及早发现重大问题
- 获得初步反馈可能需要更长时间

**混合方法(推荐):**
- 先运行快速的关键测试(兼顾两者优势)
- 然后运行较慢的关键测试
- 再运行快速的非关键测试
- 最后运行较慢的非关键测试

## 分析不同项目类型

### Python (pytest)

**分析测试结构:**

```bash
# Find all test files
find . -name "test_*.py" -o -name "*_test.py"

# Get test functions
pytest --collect-only -q

# Analyze imports in tests
grep -r "^import\|^from" tests/
```

### JavaScript/TypeScript (Jest)

**分析测试结构:**

```bash
# Find test files
find . -name "*.test.js" -o -name "*.spec.js"

# Get test list
npm test -- --listTests

# Analyze coverage
npm test -- --coverage --coverageReporters=json
```

### Java (JUnit)

**分析测试结构:**

```bash
# Find test classes
find . -name "*Test.java"

# Run with verbose output
mvn test -X

# Get test reports
cat target/surefire-reports/*.xml
```

## 最佳实践

1. **定期更新优先级排序** - 代码结构发生重大变化时重新分析
2. **结合覆盖率工具** - 有实际覆盖率数据时使用这些数据
3. **考虑测试稳定性** - 降低不稳定测试的优先级或先修复它们
4. **平衡速度与覆盖范围** - 不要牺牲重要但较慢的测试
5. **沟通排序理由** - 向团队解释测试优先级的确定原因
6. **根据场景调整** - 提交前、CI 和每夜运行需要不同的优先级
7. **监测效果** - 跟踪优先运行的测试是否更早发现了缺陷
8. **保持简单** - 从基于变更影响的基本优先级排序开始
9. **记录关键路径** - 维护高优先级领域的列表
10. **尽可能自动化** - 集成到 CI/CD 工作流中

## 场景示例

### 场景 1:提交前验证

**背景:** 开发者修改了 `src/auth/login.py`

**优先级排序:**
```
Top 3 tests to run (30 seconds):
1. tests/test_auth.py::test_login_success
2. tests/test_auth.py::test_login_failure
3. tests/test_auth.py::test_session_creation

Reasoning: Direct tests of changed functionality, fast execution.
```

### 场景 2:CI/CD 快速通道

**背景:** 拉取请求修改了 3 个模块中的 15 个文件

**优先级排序:**
```
Stage 1 - Critical Tests (2 min):
- All auth tests (5 tests)
- All payment tests (4 tests)
- Core user model tests (3 tests)

Stage 2 - Integration Tests (5 min):
- User flow integration (8 tests)
- API integration (6 tests)

Stage 3 - Full Suite (parallel, 10 min):
- Remaining tests (120 tests)

Reasoning: Fast feedback on critical paths, comprehensive validation in parallel.
```

### 场景 3:时间受限的测试

**背景:** 距离截止时间还有 5 minutes,需要验证变更

**优先级排序:**
```
Must-run tests (2 min):
1. Tests directly covering changed files (8 tests)
2. Integration tests for critical paths (4 tests)

Nice-to-run tests (3 min):
3. Related unit tests (15 tests)

Skip for now:
- E2E tests (too slow)
- Unrelated tests (no impact)

Reasoning: Maximum validation coverage in minimum time.
```

## 资源

- **`references/prioritization_algorithms.md`** - 计算优先级分数的详细算法和公式
- **`references/framework_guides.md`** - 针对特定框架的测试分析命令(pytest、jest、junit 等)

## 快速参考

| 场景 | 优先级排序策略 | 推荐测试 |
|----------|------------------|-------------------|
| 提交前 | 代码变更影响 | 针对修改文件的直接测试 |
| CI 快速通道 | 关键路径 + 变更 | 高优先级 + 受变更影响的测试 |
| 时间受限 | 仅最高分 | 按优先级分数排名前 10-20 的测试 |
| 重构后 | 广泛覆盖 | 所有涉及重构代码的测试 |
| 每夜构建 | 全面覆盖 | 所有测试(不排序) |

同类提示词

营销与销售

Seedance品牌社媒短视频策划

提供现有品牌资料,按场景、主体、动作、运镜、风格和声音设计短视频,配套发布文案、分镜和参考图。

# 社交媒体视频创作者 **端到端流程:品牌文件 → 分镜脚本 → 参考图 → Seedance 2.0 视频。** 读取你的品牌形象、ICP 和传播信息文档,制作符合品牌调性的社交视频,并针对 Seedance 2.0 的指令式提示词语法和目标平台进行全面优化。 --- ## 智能体执行协议 ### 第 1 步 — 读取品牌文件 开始撰写任何内容之前,智能体必须读取所有可用的品牌文件。在工作目录或任何 `brand/` 子目录中查找: | 文件 | 提取内容 | |:---|:---| | `brand-identity.md` | 视觉
可填 6 项需参考图
营销与销售

三风格绘画描述生成助手

输入画面构想或文案,生成三套Midjourney英文绘图指令,附中文释义、构图光影和参数。

## AI绘画提示词生成器 ### 提示词正文 ```markdown # Role: AI绘画提示词生成器·视觉艺术导演 ## Profile - Author: 姚金刚 - Version: 2.0 - Language: 中文/English - Description: 你是一位精通Midjourney (V6) 和 Stable Diffusion 提示词工程的视觉艺术导演。你能够将用户抽象的概念、文案或需求,转化为精准、高质量的AI绘画提示词(Prompt)。你深谙光影、构图、镜头语言、艺术风格和渲染引擎参数,确保生成的图片具有极高的审
中文原创
营销与销售

抖音短视频与直播增长策划

为账号规划前三秒钩子、完播节奏和系列选题,并结合热点、投流和直播电商设计运营动作。

# 抖音营销策略师 ## 你的身份与记忆 - **角色**:抖音(中国的 TikTok)短视频营销与直播电商策略专家 - **性格**:节奏感强、对数据敏锐、创意爆发力强、执行优先 - **记忆**:你记得每条播放量破百万视频的结构、每次直播流量激增的根本原因,以及每次被算法限流的惨痛教训 - **经验**:你知道抖音的核心不是“拍出漂亮的视频”,而是“在前 3 seconds 抓住注意力,让算法替你分发” ## 核心使命 ### 短视频内容策划 - 设计高完播率的视频结构:黄金 3-second 钩子 + 信息密度 + 结尾悬念 - 策划内容矩
营销与销售

B站中长视频脚本策划助手

从选题延伸到五至二十分钟脚本、标题和封面,融入分区文化、弹幕互动点和评论引导。

## B站UP主助手 ### 提示词正文 ```markdown # Role: B站UP主助手·中长视频内容创作专家 ## Profile - Author: 姚金刚 - Version: 2.0 - Language: 中文 - Description: 你是一位资深的B站内容创作专家,深度理解B站社区文化、用户偏好和推荐机制。你能够帮助UP主创作符合B站调性的中长视频内容,从选题策划到脚本撰写,从标题封面到互动设计,全方位提升内容质量。你熟悉B站各分区的特点,擅长创作有深度、有梗、有个性的B站风格内容。 ## Background ###
中文原创
营销与销售

微信视频号社交推荐与直播运营

围绕好友点赞、转发和社群分享策划短视频与直播,打通公众号、企微沉淀和小程序成交。

# 微信视频号运营策略师 你是**微信视频号运营策略师**,一位深耕微信视频号生态的内容与增长专家。你精通视频号独特的社交推荐机制,能够策划适合视频号调性的短视频和直播内容,并通过公众号、朋友圈、小程序、企业微信的生态联动,构建从内容种草到私域转化的完整商业闭环。 ## 你的身份与记忆 - **角色**:微信视频号生态全链路运营策略师 - **个性**:重视长期价值、社交裂变敏感、数据冷静、内容有温度 - **记忆**:你记住每一条通过社交推荐破百万播放的视频背后的传播链路、每一场视频号直播从冷启动到单场 GMV 破百万的关键转折、每一次因为内容调
中文原创
营销与销售

海外创作者短视频合作简报

提供产品、创作者类型、内容目标、预算和交期,生成涵盖拍摄要求、交付规格、授权与效果追踪的简报。

# UGC / EGC / KOC 内容简报(全球) ## 信息收集 制作简报前,最多提出 4 个问题: 1. **什么产品 / 服务?** 简要描述、USP、价格、目标客户。 2. **哪种创作者类型?** UGC(真实客户)、EGC(员工)、KOC(付费创作者)。默认:UGC。 3. **内容目标?** TOFU(提升认知)、MOFU(说服受众)、BOFU(促成转化)。需要的视频数量。 4. **预算和时间安排?** 每条视频的预算、视频交付截止时间、活动持续时间。 --- ## 核心原则 ### 区分 3 种创作者类型 | 标准 |
可填 5 项
✓