mcpField
T
testgen

testgen

github.com/johnlindquist测试与质量2639 次安装45 周前更新
代码仓库

关于 testgen

这项技能使用 Gemini 生成测试并管理测试套件,面向需要补充或检查测试的开发者。它涵盖单元、集成和回归测试流程,以及常见 JavaScript 测试运行器、覆盖率分析和变异测试。

主要功能

  1. 01使用 Gemini 生成测试
  2. 02支持单元、集成和回归测试流程
  3. 03使用 Jest、Vitest 或 Mocha 运行测试
  4. 04分析覆盖率和未覆盖代码
  5. 05使用 Stryker 执行变异测试

适用场景

  1. 01为新函数或功能起草测试
  2. 02在修复错误后创建回归测试
  3. 03查找未覆盖代码并获取测试建议
  4. 04检查测试能否捕获代码变更

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/johnlindquist/claude --skill testgen

安装后在 Claude Code 中输入/testgen确认已加载。

详细介绍

测试生成器

AI 驱动的测试生成与测试套件管理。

前置条件

# Gemini for AI generation
pip install google-generativeai
export GEMINI_API_KEY=your_api_key

# Test runners (install as needed)
npm install -D jest vitest mocha
npm install -D @stryker-mutator/core  # Mutation testing

AI 测试生成

生成单元测试
# Read code and generate tests
CODE=$(cat src/utils.ts)

gemini -m pro -o text -e "" "Generate comprehensive unit tests for this code:

$CODE

Requirements:
1. Use Jest/Vitest syntax
2. Cover happy path, edge cases, and error conditions
3. Include descriptive test names
4. Mock external dependencies
5. Test each exported function

Output only the test code, ready to save to a file."
为特定函数生成测试
gemini -m pro -o text -e "" "Generate unit tests for this function:

\`\`\`typescript
export function calculateDiscount(price: number, percentage: number): number {
  if (percentage < 0 || percentage > 100) {
    throw new Error('Invalid percentage');
  }
  return price * (1 - percentage / 100);
}
\`\`\`

Include tests for:
- Valid inputs
- Boundary values (0%, 100%)
- Invalid inputs (negative, >100)
- Edge cases (zero price)

Use Jest syntax."
TDD 循环
# 1. Write failing test first
gemini -m pro -o text -e "" "Write a failing test for: [feature description]"

# 2. Run test (should fail)
npm test -- --watch

# 3. Implement minimal code to pass

# 4. Refactor

# 5. Repeat

测试运行器

Jest
# Run all tests
npx jest

# Watch mode
npx jest --watch

# Specific file
npx jest src/utils.test.ts

# With coverage
npx jest --coverage

# Update snapshots
npx jest -u

# Verbose output
npx jest --verbose

# Run matching pattern
npx jest -t "should calculate"
Vitest
# Run tests
npx vitest

# Watch mode
npx vitest --watch

# Run once
npx vitest run

# Coverage
npx vitest --coverage

# UI mode
npx vitest --ui

# Specific file
npx vitest src/utils.test.ts
Mocha
# Run tests
npx mocha

# Watch mode
npx mocha --watch

# Specific file
npx mocha test/utils.test.js

# With reporter
npx mocha --reporter spec

覆盖率分析

生成覆盖率报告
# Jest
npx jest --coverage --coverageReporters=text --coverageReporters=html

# Vitest
npx vitest run --coverage

# View HTML report
open coverage/index.html
覆盖率阈值
// jest.config.js or vitest.config.ts
{
  "coverageThreshold": {
    "global": {
      "branches": 80,
      "functions": 80,
      "lines": 80,
      "statements": 80
    }
  }
}
查找未覆盖的代码
# Text coverage report
npx jest --coverage --coverageReporters=text

# Ask AI for suggestions
UNCOVERED=$(npx jest --coverage --coverageReporters=json --silent | jq '.coverageMap')
gemini -m pro -o text -e "" "Given this coverage report, suggest tests for uncovered code:

$UNCOVERED"

变异测试

使用 Stryker 测试测试本身:

# Check if installed
npx stryker --version

# Initialize
npx stryker init

# Run mutation testing
npx stryker run

# With reporters
npx stryker run --reporters clear-text,json,html
解读结果
  • 被杀的变异体:测试捕获了 bug(良好)
  • 存活的变异体:测试漏掉了 bug(需要更多测试)
  • 超时的变异体:变异体导致了无限循环
  • 无覆盖率:代码未被任何测试覆盖

工作流模式

新功能测试
# 1. Generate test scaffold
CODE=$(cat src/new-feature.ts)
gemini -m pro -o text -e "" "Generate test file scaffold for: $CODE" > src/new-feature.test.ts

# 2. Run tests (should fail)
npx jest src/new-feature.test.ts

# 3. Implement feature
# 4. Run tests (should pass)
# 5. Add edge case tests
# 6. Check coverage
npx jest src/new-feature.test.ts --coverage
回归测试生成
# After fixing a bug, generate regression test
BUG_DESC="Users could submit empty forms"
FIX=$(git diff HEAD~1)

gemini -m pro -o text -e "" "Generate a regression test for this bug fix:

Bug: $BUG_DESC

Fix:
$FIX

The test should fail if the bug is reintroduced."
集成测试生成
gemini -m pro -o text -e "" "Generate integration tests for this API endpoint:

\`\`\`typescript
// POST /api/users
// Body: { name: string, email: string }
// Response: { id: string, name: string, email: string }
\`\`\`

Include:
- Success case
- Validation errors
- Duplicate email handling
- Authentication required

Use supertest or similar."

最佳实践

  1. 测试行为,而非实现 - 关注做什么,而非怎么做
  2. 每个测试一个断言 - 在可行的情况下
  3. 描述性的名称 - 应说明所测试的内容
  4. Arrange-Act-Assert - 清晰的测试结构
  5. Mock 外部依赖 - 隔离被测单元
  6. 在 CI 中运行测试 - 自动化验证
  7. 维护覆盖率 - 不要让覆盖率下降
  8. 使用变异测试 - 验证测试质量

评论

支持 Markdown