SuperAPI AI 工具集

testing-e2e

测试与质量

查看项目

更新于 10-16 06:57

技能介绍

用途

该技能用于端到端(E2E)测试,涵盖完整的 E2E 测试套件、API 测试、负载测试以及测试报告生成。

主要能力

  • Playwright E2E:运行完整 E2E 套件、带重试运行、CI 模式(全浏览器、并行)运行、按标签(如 @smoke、@critical)筛选运行,并在失败时生成追踪用于调试。
  • Cypress E2E:运行全部或指定 E2E 用例、录制视频、指定浏览器(如 electron)、通过环境变量传入 BASE_URL。
  • API 测试(supertest):通常配合 Jest 或 Vitest 运行 API 测试,并可搭建 API 测试示例(如创建 tests/api/health.test.ts,导入 app 并使用 request(app).get("/health"))。
  • k6 负载测试:运行 k6 负载测试(需已安装 k6),支持自定义虚拟用户数(VUs)与时长、阈值、JSON 输出,并可搭建 k6 脚本示例(如创建 load-tests/smoke.js,导入 k6/http,定义 options:vus、duration、thresholds,导出默认函数使用 http.get 与 check)。
  • 测试报告生成:Playwright 支持 HTML、JSON、JUnit 报告;Cypress 支持 mochawesome 报告并可合并多个报告;Jest 支持 HTML 报告(需 jest-html-reporter)。

使用方式

运行 Playwright E2E 套件

# 运行完整 E2E 套件
npx playwright test --config=playwright.config.ts

# 带重试运行 E2E 测试
npx playwright test --retries=2

# CI 模式运行 E2E(全浏览器、并行)
npx playwright test --reporter=html --workers=4

# 运行带标签的 E2E 测试
npx playwright test --grep="@smoke"
npx playwright test --grep="@critical"

失败时追踪用于调试:

npx playwright test --trace=on-first-retry
npx playwright show-trace test-results/trace.zip

运行 Cypress E2E

# 运行所有 E2E 用例
npx cypress run --e2e

# 运行指定 E2E 用例
npx cypress run --spec "cypress/e2e/checkout.cy.ts"

# 运行并录制视频
npx cypress run --e2e --record

# 在指定浏览器中运行
npx cypress run --e2e --browser electron

# 使用环境变量运行
npx cypress run --e2e --env BASE_URL=http://localhost:3000

使用 supertest 进行 API 测试

# 运行 API 测试(通常为 Jest 或 Vitest)
npx jest tests/api/
npx vitest run tests/api/

# 搭建 API 测试(示例)
# 创建 tests/api/health.test.ts 并使用 supertest
# 导入 app 并使用 request(app).get("/health")

使用 k6 进行负载测试

# 运行 k6 负载测试(需已安装 k6)
k6 run load-tests/smoke.js

# 使用自定义 VUs 和时长运行
k6 run --vus 50 --duration 30s load-tests/stress.js

# 使用阈值运行
k6 run --out json=results.json load-tests/spike.js

# 搭建 k6 脚本(示例)
# 创建 load-tests/smoke.js 并导入 k6/http
# 定义 options:vus、duration、thresholds
# 导出默认函数,使用 http.get 与 check

测试报告生成

# Playwright HTML 报告
npx playwright test --reporter=html
npx playwright show-report

# Playwright JSON 报告
npx playwright test --reporter=json > test-results.json

# Playwright JUnit 报告(用于 CI)
npx playwright test --reporter=junit > junit-results.xml

# 使用 mochawesome 生成 Cypress 报告
npx cypress run --reporter mochawesome --reporter-options reportDir=reports,overwrite=false,html=true

# 合并多个 mochawesome 报告
npx mochawesome-merge reports/*.json > merged-report.json
npx marge merged-report.json --reportDir=reports --inline

# Jest HTML 报告(需 jest-html-reporter)
npx jest --reporters=default --reporters=jest-html-reporter

适用场景

适用于需要对 Web 应用进行端到端验证、API 接口测试、系统负载与压力测试,以及生成可视化或机器可读测试报告的开发与测试流程。

限制

k6 负载测试需预先安装 k6;Jest HTML 报告需安装 jest-html-reporter;Cypress 报告使用 mochawesome 相关工具。其余具体限制暂未提供。