louayhouimli /
testgenie-ai
AI-powered CLI test generator that creates comprehensive test suites for your JavaScript/TypeScript projects in seconds.
Loading repository data…
seyseyseyz / repository
AI-powered test generator for JavaScript/TypeScript projects
🤖 AI驱动的单元测试生成器,具有智能优先级评分和代码质量验证
npm install -g ai-unit-test-generator
npm install --save-dev ai-unit-test-generator
ai-test init
这将在当前目录创建 ai-test.config.jsonc 配置文件,包含详细的注释说明。
# 生成独立的 best_practices.md 文件
ai-test init-best-practices
# 或者生成内联配置(嵌入到 ai-test.config.jsonc)
ai-test init-best-practices --inline
这将:
ai-test scan
执行流程:
reports/ut_scores.md)示例输出:
| Status | Score | Priority | Name | Type | Layer | Path | Coverage | Testability | Complexity |
|--------|-------|----------|------|------|-------|------|----------|-------------|------------|
| TODO | 8.5 | P0 | validatePayment | function | Business | src/services/payment.ts | 0.0% | 9 | 10 |
| TODO | 7.8 | P0 | formatCurrency | function | Foundation | src/utils/format.ts | 15.0% | 10 | 8 |
# 生成前10个最高优先级的函数测试(带自动改进)
ai-test generate
# 生成前20个函数的测试
ai-test generate -n 20
# 只生成 P0 优先级的测试
ai-test generate -p P0
# 生成所有 TODO 函数的测试
ai-test generate --all
# 禁用自动改进(单次生成模式)
ai-test generate --no-iterative
执行流程:
# 查看完整报告
cat reports/ut_scores.md
# 查看 P0 优先级任务
grep "| P0 |" reports/ut_scores.md
# 查看待处理任务
grep "| TODO |" reports/ut_scores.md
ai-test init初始化配置文件
ai-test init [options]
Options:
-c, --config <path> Config file path (default: ai-test.config.jsonc)
ai-test init-best-practices 🆕生成项目特定的测试规范
ai-test init-best-practices [options]
Options:
--inline Generate inline config instead of separate file
-c, --config <path> Config file path (default: ai-test.config.jsonc)
两种模式:
best_practices.md--inline): 嵌入到 ai-test.config.jsoncai-test scan扫描代码并生成优先级报告
ai-test scan [options]
Options:
-c, --config <path> Config file path (default: ai-test.config.jsonc)
-o, --output <dir> Output directory (default: reports)
--skip-git Skip Git history analysis
输出文件:
reports/targets.json - 扫描的目标列表reports/git_signals.json - Git 分析数据reports/ut_scores.md - Markdown 格式报告(按评分排序)reports/ut_scores.csv - CSV 格式报告ai-test generate生成单元测试(使用 Cursor Agent)
ai-test generate [options]
Options:
-n, --count <number> Number of functions to generate (default: 10)
-p, --priority <level> Priority filter (P0, P1, P2, P3)
--all Generate all remaining TODO functions
--no-iterative Disable iterative improvement (default: enabled)
--max-iterations <number> Maximum iterations for iterative mode (default: 3)
--report <path> Report file path (default: reports/ut_scores.md)
示例:
ai-test generate # 生成前10个,带自动改进
ai-test generate -n 20 # 生成前20个
ai-test generate -p P0 # 只生成 P0 优先级
ai-test generate --all # 生成所有 TODO 函数
ai-test generate --no-iterative # 禁用自动改进
首次运行 ai-test scan 时,会自动生成包含详细注释的配置文件 ai-test.config.jsonc。
{
"version": "3.1.0",
"scoringMode": "layered", // 评分模式: layered 或 legacy
// 覆盖率配置
"coverage": {
"runBeforeScan": true, // 扫描前自动运行覆盖率分析
"command": "npx jest --coverage --silent"
},
// 覆盖率评分映射
"coverageScoring": {
"naScore": 5, // 无覆盖率数据时的默认分数
"mapping": [
{ "lte": 0, "score": 10 }, // 0% 覆盖率 → 最高优先级
{ "lte": 40, "score": 8 }, // 1-40% → 高优先级
{ "lte": 70, "score": 6 }, // 41-70% → 中等优先级
{ "lte": 90, "score": 3 }, // 71-90% → 低优先级
{ "lte": 100, "score": 1 } // 91-100% → 最低优先级
]
},
// v3.1.0 新增配置
"bestPractices": {
"enabled": false,
"source": "file", // "file" 或 "inline"
"filePath": "./best_practices.md"
},
"validation": {
"enabled": false,
"maxAttempts": 3,
"timeout": 30000
},
"coverageDriven": {
"enabled": false,
"targetCoverage": 80,
"maxIterations": 5
},
"deduplication": {
"enabled": false,
"similarityThreshold": 0.85
}
}
{
"layers": {
"foundation": {
"name": "Foundation (基础工具层)",
"patterns": ["**/utils/**", "**/helpers/**", "**/constants/**"],
"weights": {
"testability": 0.45, // 可测试性权重
"dependencyCount": 0.25, // 依赖数量权重
"complexity": 0.20, // 复杂度权重
"coverage": 0.10 // 覆盖率权重
},
"thresholds": {
"P0": 8.0, // 分数 ≥8.0 = P0 (必须测试)
"P1": 6.5, // 分数 6.5-7.9 = P1 (高优先级)
"P2": 5.0 // 分数 5.0-6.4 = P2 (中等优先级), <5.0 = P3
},
"coverageTarget": 100 // 目标覆盖率 100%
}
// ... 其他层级配置
}
}
| 优先级 | 分数范围 | 描述 | 建议操作 |
|---|---|---|---|
| P0 | ≥8.0 | 必须测试 | 立即生成 |
| P1 | 6.5-7.9 | 高优先级 | 批量生成 |
| P2 | 5.0-6.4 | 中等优先级 | 审查后生成 |
| P3 | <5.0 | 低优先级 | 可选覆盖 |
新特性: 集成代码覆盖率数据,适用于增量和现有代码场景
基于引用计数:
# 自动生成测试(内置 Cursor Agent 集成)
ai-test generate -n 10
# 1. 生成 AI prompt
ai-test scan
grep "| TODO |" reports/ut_scores.md | head -10
# 2. 手动复制函数信息到 AI 工具
# 3. 保存 AI 响应到文件
# 4. 运行测试验证
npm test
# 1. 安装 Jest(如果尚未安装)
npm i -D jest@29 ts-jest@29 @types/jest@29 jest-environment-jsdom@29
# 2. 初始化配置
ai-test init
# 3. (可选) 生成测试规范
ai-test init-best-practices
# 4. 扫描代码
ai-test scan
# ✅ 自动生成配置文件
# ✅ 自动运行覆盖率分析
# ✅ 生成优先级报告
# 5. 查看报告
cat reports/ut_scores.md
# 6. 生成测试(10个函数)
ai-test generate
# 7. 查看结果
npm test
# 8. 继续下一批
ai-test generate -n 10
# 9. 重复直到所有高优先级测试完成
首次使用需要安装 Jest:
npm i -D jest@29 ts-jest@29 @types/jest@29 jest-environment-jsdom@29
然后在 tsconfig.json 中添加类型支持:
{
"compilerOptions": {
"typeRoots": ["node_modules/@types"]
}
}
修改 ai-test.config.jsonc:
{
"coverage": {
"runBeforeScan": true,
"command": "npm run test:coverage" // 自定义命令
}
}
如果项目没有 Git 历史或不需要 Git 信号:
ai-test scan --skip-git
ai-test-generator/
├── src/ # TypeScript 源代码
│ ├── cli.ts # CLI 入口
│ ├── types/ # 类型定义 (5 模块)
│ ├── shared/ # 共享工具层 (4 模块)
│ ├── core/ # 核心分析引擎
│ │ ├── scanner.ts # AST 扫描
│ │ ├── git-analyzer.ts # Git 历史分析
│ │ ├── scoring/ # 评分系统
│ │ ├── mock/ # Mock 分析 (v3.1.0: 简化版)
│ │ └── behavior/ # 行为分类
│ ├── ai/ # AI 交互层
│ │ ├── prompt-builder.ts # Prompt 构建
│ │ ├── client.ts # Cursor Agent 调用
│ │ ├── extractor.ts # 测试提取
│ │ └── ...
│ ├── testing/ # 测试执行层
│ │ ├── runner.ts # Jest 运行器
│ │ ├── validator.ts # 🆕 v3.1.0: 测试验证器
│ │ ├── deduplicator.ts # 🆕 v3.1.0: 测试去重
│ │ └── ...
│ ├── workflows/ # 工作流编排层
│ │ ├── init.ts # 初始化
│ │ ├── scan.ts # 扫描
│ │ ├── generate.ts # 生成
│ │ ├── init-best-practices.ts # 🆕 v3.1.0: Best Practices
│ │ ├── generate-with-validation.ts # 🆕 v3.1.0: 带验证的生成
│ │ ├── coverage-driven-generate.ts # 🆕 v3.1.0: 覆盖率驱动
│ │ └── ...
│ └── utils/ # 工具层
│ ├── config-manager.ts # 配置管理
│ └── ...
├── dist/ # 编译后的代码
├── templates/ # 配置模板
│ ├── default.config.jsonc # 默认配置
│ └── jest.config.cobertura.js
└── package.json
移除理由:
新增理由:
本项目从以下研究和实践中汲取灵感:
Meta TestGen-LLM: 质量保证过滤器和大规模实践
Automated Unit Test Improvement using Large Language Models at Meta
Qodo Cover: Best Practices、实时验证
https://www.qodo.ai/
Keploy: 测试去重、覆盖率驱动
https://keploy.io/
ByteDance Midscene.js: 自然语言接口和稳定性实践
https://github.com/web-infra-dev/midscene
这些理念在 ai-test-generator 中体现为:
欢迎贡献!请提交问题或拉取请求。
MIT © YuhengZhou
需要帮助?通过以下方式获取支持:
提示: 首次使用建议先在小项目上测试,熟悉工作流后再应用到大项目。
Selected from shared topics, language and repository description—not editorial ratings.
louayhouimli /
AI-powered CLI test generator that creates comprehensive test suites for your JavaScript/TypeScript projects in seconds.
GiriGummadi /
AI-powered QA Test Case Generator from SRS documents and UI screenshots using OpenAI API and Python
srinidhi-anand /
AI-Powered Jest Test Case Generator for TypeScript (Node.js)
git-senpai /
AI-Powered Test Case Generator – A full-stack web application that integrates with GitHub and uses AI (OpenAI, Gemini, or Ollama) to automatically generate unit, integration, and edge-case tests for your source code. Supports multiple languages and testing frameworks with a modern, responsive UI for seamless developer experience.
meetpotdar777 /
An interactive full-stack portal to design, test, and generate AI-powered API documentation and READMEs.
jay-yeluru /
A hands-on guide to using Playwright's AI-powered Planner, Generator, and Healer agents to autonomously create, run, and self-heal tests using MCP (Model Context Protocol).