ai-testgen-tool
v1.0.0
Published
AI-powered CLI tool to generate unit, service, integration, and end-to-end tests for service codebases.
Maintainers
Readme
AI-TestGen
AI-TestGen is a CLI tool that leverages AI to automatically generate unit, service, integration, and end-to-end tests for service-oriented codebases (Node.js, TypeScript, etc.).
Purpose
- Save developer time and effort
- Improve test coverage and quality
- Reduce bugs and regressions
- Standardize testing practices across teams
Features
- Code analysis and test suggestion
- Test code generation (unit, service, integration, E2E)
- Customizable test templates
- Review and edit interface
- Integration with code editors and CI/CD
- Flexible output directory and filename options
- Overwrite protection and auto-directory creation
- Dry run mode: Preview what would be generated without writing files
- Optional LLM/AI-powered test generation: Use GPT (OpenAI) to generate tests if desired, with support for custom endpoints/gateways
Quickstart
- Install dependencies:
npm install - Generate tests for a TypeScript service:
node src/cli/index.js generate --all src/userService.ts --output tests/generated - Preview what would be generated (dry run):
node src/cli/index.js generate --all src/userService.ts --output tests/generated --dry-run - Specify custom output directories and filenames:
node src/cli/index.js generate --all src/userService.ts \ --unit-output tests/unit --unit-filename userService.unit.test.ts \ --service-output tests/service --service-filename userService.service.test.ts \ --integration-output tests/integration --integration-filename userService.integration.test.ts \ --e2e-output tests/e2e --e2e-filename userService.e2e.test.ts - Overwrite existing files:
node src/cli/index.js generate --all src/userService.ts --output tests/generated --force
LLM/AI-Powered Test Generation (Optional)
If you want to use OpenAI GPT to generate tests:
- Set your API key:
export OPENAI_API_KEY=sk-... - (Optional) Set a custom OpenAI-compatible base URL (for gateways, Azure, etc.):
export OPENAI_BASE_URL=https://your-gateway.example.com/v1 # or use the CLI flag: # --openai-base-url https://your-gateway.example.com/v1 - Run with the --ai flag:
node src/cli/index.js generate --all src/userService.ts --output tests/generated --ai # or with a custom base URL: node src/cli/index.js generate --all src/userService.ts --output tests/generated --ai --openai-base-url https://your-gateway.example.com/v1
- If no API key is set, or --ai is not used, the tool falls back to static/template-based generation.
- Each developer must use their own API key. Never share your key.
CLI Usage
Usage: ai-testgen generate [type] <target> [options]
Arguments:
[type] Test type: unit | service | integration | e2e (optional if --all is used)
<target> Target file or directory
Options:
-o, --output <dir> Default output directory (default: "examples")
--all Generate all test types for the target file (omit [type] argument)
--unit-output <dir> Unit test output directory
--unit-filename <name> Unit test filename
--service-output <dir> Service test output directory
--service-filename <name> Service test filename
--integration-output <dir> Integration test output directory
--integration-filename <name> Integration test filename
--e2e-output <dir> E2E test output directory
--e2e-filename <name> E2E test filename
--force Overwrite existing test files
--dry-run Show what would be generated, but do not write files
--ai Use LLM-powered test generation if OPENAI_API_KEY is set
--openai-base-url <url> Custom OpenAI-compatible base URL (for gateways, Azure, etc.)Real-World Examples
Preview all test types with custom locations (dry run, no [type] needed):
node src/cli/index.js generate --all src/userService.ts \
--unit-output tests/unit --unit-filename userService.unit.test.ts \
--service-output tests/service --service-filename userService.service.test.ts \
--integration-output tests/integration --integration-filename userService.integration.test.ts \
--e2e-output tests/e2e --e2e-filename userService.e2e.test.ts --dry-runGenerate all test types with OpenAI GPT (if API key is set):
export OPENAI_API_KEY=sk-...
node src/cli/index.js generate --all src/userService.ts --output tests/generated --aiGenerate all test types with a custom OpenAI-compatible endpoint:
export OPENAI_API_KEY=sk-...
export OPENAI_BASE_URL=https://your-gateway.example.com/v1
node src/cli/index.js generate --all src/userService.ts --output tests/generated --ai
# or
node src/cli/index.js generate --all src/userService.ts --output tests/generated --ai --openai-base-url https://your-gateway.example.com/v1Generate only a unit test in a custom location:
node src/cli/index.js generate unit src/userService.ts --unit-output tests/unit --unit-filename userService.unit.test.tsOverwrite existing files:
node src/cli/index.js generate --all src/userService.ts --output tests/generated --forcePreview (dry run) before generating:
node src/cli/index.js generate --all src/userService.ts --output tests/generated --dry-runLicense
MIT
