Files
novalon-website/docs/testing/README-TIERED-TESTING.md
T
张翔 498bb3a3c8 refactor: reorganize project structure and improve code quality
- Move CI/CD configs to config/ci/ directory
- Reorganize scripts into categorized directories (deployment, monitoring, testing, utils)
- Consolidate documentation into docs/ directory with proper structure
- Update linting and testing configurations
- Remove obsolete test reports and performance summaries
- Add new documentation for code quality tools and contact form security
- Improve project organization and maintainability
- Fix lint-staged config to only lint JS/TS files
- Disable react/react-in-jsx-scope rule for Next.js compatibility
- Ignore scripts and test config directories in ESLint
2026-03-24 13:38:58 +08:00

6.8 KiB
Raw Blame History

分层测试快速入门指南

什么是分层测试?

分层测试是一种测试策略,将测试按照执行时间和重要性分为三个层级:

  • 快速层5分钟内完成,验证核心功能
  • 标准层30分钟内完成,验证大部分功能
  • 深度层:可接受较长执行时间,进行全面验证

快速开始

1. 本地运行测试

运行快速层测试(推荐日常开发使用)

npm run test:tier:fast

运行标准层测试

npm run test:tier:standard

运行深度层测试

npm run test:tier:deep

运行所有层级测试

npm run test:tier:all

2. 编写分层测试

快速层测试示例

test.describe('API快速测试 @smoke @critical', () => {
  test('应该能够获取内容列表', async ({ request }) => {
    const response = await request.get('/api/admin/content');
    expect(response.status()).toBe(200);
  });
});

标准层测试示例

test.describe('管理后台功能测试 @admin @regression', () => {
  test('应该能够创建新闻', async ({ page }) => {
    await page.goto('/admin/news');
    await page.click('[data-testid="create-news-btn"]');
    await page.fill('[data-testid="news-title"]', '测试新闻');
    await page.click('[data-testid="save-btn"]');
    
    await expect(page.locator('[data-testid="success-message"]')).toBeVisible();
  });
});

深度层测试示例

test.describe('首页视觉回归测试 @visual @regression', () => {
  test('桌面端首页应该与基准一致', async ({ page }) => {
    await page.setViewportSize({ width: 1280, height: 720 });
    await page.goto('/');
    
    await expect(page).toHaveScreenshot('homepage-desktop.png');
  });
});

3. 使用测试标记

为测试添加标记以便分类和管理:

test.describe('测试套件 @smoke @critical', () => {
  test('测试用例 @api @regression', async ({ page }) => {
    // 测试逻辑
  });
});

常用标记:

  • @smoke - 冒烟测试
  • @critical - 关键测试
  • @regression - 回归测试
  • @visual - 视觉测试
  • @api - API测试
  • @mobile - 移动端测试

CI/CD集成

项目已配置Woodpecker CI,自动执行分层测试:

分支策略

  • main分支:执行所有层级测试
  • develop分支:执行快速层和标准层测试
  • 其他分支:仅执行快速层测试

工作流程

  1. 提交代码到分支
  2. Woodpecker CI自动触发
  3. 依次执行快速层、标准层、深度层测试
  4. 前一层失败则停止后续执行
  5. 生成测试报告并上传
  6. 发送通知

性能优化

识别慢速测试

运行性能优化工具:

cd e2e && node test-optimizer-simple-test.js

工具会生成优化报告,包含:

  • 慢速测试列表
  • 优化建议
  • 潜在时间节省

优化建议

  1. 减少等待时间

    // 不推荐
    await page.waitForTimeout(5000);
    
    // 推荐
    await page.waitForSelector('[data-testid="result"]', { timeout: 5000 });
    
  2. 使用data-testid选择器

    // 不推荐
    await page.click('div > div > button');
    
    // 推荐
    await page.click('[data-testid="submit-btn"]');
    
  3. 拆分大测试

    // 不推荐:单个大测试
    test('完整的用户注册流程', async ({ page }) => {
      // 100+ 行代码
    });
    
    // 推荐:拆分为多个小测试
    test.describe('用户注册流程', () => {
      test('应该能够填写注册表单', async ({ page }) => {
        // 20 行代码
      });
    
      test('应该能够提交注册', async ({ page }) => {
        // 20 行代码
      });
    });
    

监控和告警

测试执行历史

系统自动记录测试执行历史,存储在 e2e/test-history.json

告警规则

系统会根据以下规则触发告警:

  1. 测试通过率低于80% (Critical)
  2. 测试通过率低于90% (High)
  3. 测试执行时间超过30分钟 (Medium)
  4. 失败测试数量超过10个 (High)
  5. 深度层测试存在失败 (Critical)

查看告警

告警信息会输出到控制台,并保存在 test-results/alerts.json

常见问题

Q: 测试超时怎么办?

A: 检查以下几点:

  1. 是否有不必要的等待时间
  2. 选择器是否正确
  3. 网络请求是否正常
  4. 是否需要增加超时时间

Q: 测试不稳定怎么办?

A: 采用以下策略:

  1. 增加重试次数
  2. 使用更稳定的等待策略
  3. 检查是否有竞态条件
  4. 使用data-testid选择器

Q: 如何确定测试应该放在哪一层?

A: 根据执行时间和重要性:

  • 执行时间<30秒且是关键功能 → 快速层
  • 执行时间<60秒 → 标准层
  • 执行时间>60秒或需要完整回归 → 深度层

Q: 如何减少测试执行时间?

A: 采用以下策略:

  1. 并行执行测试
  2. 减少不必要的等待
  3. 优化选择器
  4. 拆分大测试
  5. 使用mock数据

进阶使用

自定义测试层级

编辑 e2e/src/config/test-tiers.ts

export const TEST_TIERS: Record<string, TestTierConfig> = {
  fast: {
    name: '快速层',
    description: '冒烟测试、API测试、基础功能验证',
    testMatch: /.*\.smoke\.spec\.ts$|.*\.api\.spec\.ts$/,
    timeout: 30000,
    retries: 1,
    workers: process.env.CI ? 6 : '75%',
    fullyParallel: true,
    failFast: true,
  },
  // ... 其他层级
};

添加自定义告警规则

编辑 e2e/src/utils/test-monitor.ts

this.alertRules.push({
  name: 'custom-alert',
  condition: (m) => m.failedTests > 5 && m.tier === 'fast',
  severity: 'critical',
  message: '快速层测试失败超过5个',
});

自定义优化规则

编辑 e2e/src/utils/test-optimizer.ts

this.rules.push({
  name: 'custom-rule',
  condition: (p) => p.duration > 90000 && p.tier === 'standard',
  suggestions: [
    '标准层测试不应超过90秒',
    '考虑拆分测试或优化执行流程',
  ],
});

文档资源

获取帮助

如果遇到问题:

  1. 查看文档资源
  2. 检查测试日志
  3. 运行性能优化工具
  4. 联系团队成员

总结

分层测试系统通过以下方式提高测试效率:

  1. 快速反馈:快速层测试在5分钟内完成
  2. 合理分配:根据重要性分配测试资源
  3. 持续优化:通过历史数据持续优化
  4. 自动化CI/CD自动执行和报告

开始使用分层测试,提高测试效率,缩短反馈周期!