test-coverage-analyzer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Test Coverage Analyzer

测试覆盖率分析器

Analyze test coverage and identify testing gaps.
分析测试覆盖率并识别测试缺口。

Quick Start

快速开始

Run coverage and analyze results:
bash
undefined
运行覆盖率检测并分析结果:
bash
undefined

JavaScript/TypeScript

JavaScript/TypeScript

npm test -- --coverage
npm test -- --coverage

Python

Python

pytest --cov=src --cov-report=term-missing
pytest --cov=src --cov-report=term-missing

Go

Go

go test -cover ./...
undefined
go test -cover ./...
undefined

Instructions

操作步骤

Step 1: Generate Coverage Report

步骤1:生成覆盖率报告

JavaScript/TypeScript (Jest):
bash
npm test -- --coverage --coverageReporters=text --coverageReporters=lcov
JavaScript/TypeScript (Vitest):
bash
vitest run --coverage
Python (pytest):
bash
pytest --cov=src --cov-report=html --cov-report=term-missing
Go:
bash
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
JavaScript/TypeScript (Jest):
bash
npm test -- --coverage --coverageReporters=text --coverageReporters=lcov
JavaScript/TypeScript (Vitest):
bash
vitest run --coverage
Python (pytest):
bash
pytest --cov=src --cov-report=html --cov-report=term-missing
Go:
bash
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

Step 2: Analyze Coverage Metrics

步骤2:分析覆盖率指标

Review these key metrics:
  • Line coverage: Percentage of lines executed
  • Branch coverage: Percentage of conditional branches tested
  • Function coverage: Percentage of functions called
  • Statement coverage: Percentage of statements executed
Target thresholds:
  • Critical code: 80%+ coverage
  • Standard code: 60%+ coverage
  • UI/Config: 40%+ coverage
查看以下关键指标:
  • 行覆盖率:已执行代码行的百分比
  • 分支覆盖率:已测试条件分支的百分比
  • 函数覆盖率:已调用函数的百分比
  • 语句覆盖率:已执行语句的百分比
目标阈值:
  • 核心代码:覆盖率≥80%
  • 标准代码:覆盖率≥60%
  • UI/配置代码:覆盖率≥40%

Step 3: Identify Gaps

步骤3:识别测试缺口

Look for:
  • Uncovered functions: Functions with 0% coverage
  • Partial coverage: Functions with <50% coverage
  • Missing branches: Untested if/else paths
  • Error paths: Untested catch blocks
  • Edge cases: Boundary conditions not tested
重点关注:
  • 未覆盖函数:覆盖率为0%的函数
  • 部分覆盖函数:覆盖率<50%的函数
  • 缺失分支:未测试的if/else路径
  • 错误处理路径:未测试的catch块
  • 边缘场景:未测试的边界条件

Step 4: Prioritize Testing

步骤4:划分测试优先级

High Priority (test first):
  • Business logic with 0% coverage
  • Security-critical functions
  • Payment/transaction code
  • Data validation logic
  • Error handling paths
Medium Priority (test next):
  • API endpoints
  • Database operations
  • Utility functions
  • Configuration logic
Low Priority (test if time permits):
  • Simple getters/setters
  • UI presentation logic
  • Type definitions
  • Generated code
高优先级(优先测试):
  • 覆盖率为0%的业务逻辑
  • 安全关键函数
  • 支付/交易相关代码
  • 数据验证逻辑
  • 错误处理路径
中优先级(次之测试):
  • API接口
  • 数据库操作
  • 工具函数
  • 配置逻辑
低优先级(时间允许时测试):
  • 简单的getter/setter方法
  • UI展示逻辑
  • 类型定义
  • 自动生成的代码

Step 5: Create Action Plan

步骤5:制定行动计划

For each gap:
  1. Identify the untested code
  2. Determine test type needed (unit/integration/e2e)
  3. Estimate effort (small/medium/large)
  4. Assign priority (high/medium/low)
  5. Create test implementation tasks
针对每个测试缺口:
  1. 定位未测试代码
  2. 确定所需测试类型(单元测试/集成测试/端到端测试)
  3. 评估工作量(小/中/大)
  4. 划分优先级(高/中/低)
  5. 创建测试实现任务

Common Patterns

常见模式

Pattern: Find untested files
bash
undefined
模式:查找未测试文件
bash
undefined

Jest

Jest

npm test -- --coverage --collectCoverageFrom='src/**/*.{js,ts}' --coverageThreshold='{"global":{"lines":0}}'
npm test -- --coverage --collectCoverageFrom='src/**/*.{js,ts}' --coverageThreshold='{"global":{"lines":0}}'

pytest

pytest

pytest --cov=src --cov-report=term-missing | grep "0%"

**Pattern: Check specific module**
```bash
pytest --cov=src --cov-report=term-missing | grep "0%"

**模式:检查特定模块**
```bash

Jest

Jest

npm test -- path/to/module --coverage
npm test -- path/to/module --coverage

pytest

pytest

pytest tests/test_module.py --cov=src.module --cov-report=term-missing

**Pattern: Enforce coverage thresholds**
```json
// package.json (Jest)
{
  "jest": {
    "coverageThreshold": {
      "global": {
        "branches": 70,
        "functions": 70,
        "lines": 70,
        "statements": 70
      }
    }
  }
}
pytest tests/test_module.py --cov=src.module --cov-report=term-missing

**模式:强制执行覆盖率阈值**
```json
// package.json (Jest)
{
  "jest": {
    "coverageThreshold": {
      "global": {
        "branches": 70,
        "functions": 70,
        "lines": 70,
        "statements": 70
      }
    }
  }
}

Advanced

进阶内容

For detailed information, see:
  • Coverage Tools - Tool-specific configuration and usage
  • Testing Patterns - Common testing patterns and best practices
如需详细信息,请查看:
  • Coverage Tools - 工具专属配置与使用指南
  • Testing Patterns - 常见测试模式与最佳实践