TestGenAI
✨ A CLI gem that adds tests for untested methods in your code ✨
TestGenAI is a Ruby CLI gem for development environments that finds untested methods in your codebase and generates tests for them using an AI service.
Caution
TestGenAI is a work in progress and should not be used in production environments. While is it works locally for me, it has not been tested in a wide variety of projects yet and may have many edge cases or bugs. Use with caution and please report any issues you encounter.
See this article for the motivation and design behind TestGenAI.
Installation
Add to your project's Gemfile in the development group, or install the gem directly:
gem install testgenaiSetup
TestGenAI needs an LLM provider to generate tests. Configure it via environment variables or CLI flags:
| Env var | Flag | Description |
|---|---|---|
TESTGENAI_PROVIDER |
--provider |
LLM provider (e.g. anthropic, openai) |
TESTGENAI_MODEL |
--model |
Model name (e.g. claude-opus-4-7) |
| — | --api-key |
API key (overrides env var set by the provider SDK) |
TESTGENAI_FRAMEWORK |
--test-framework, -t
|
Test framework: rspec (default) or minitest
|
TESTGENAI_OUTPUT_DIR |
--output-dir, -o
|
Output directory for generated tests |
TESTGENAI_PAUSE |
--pause, -p
|
Seconds to pause between API calls (default: 1) |
| — | --conventions |
Analyze project test patterns and include them in prompts |
Commands
testgenai scan Scan for untested methods and report them
testgenai context Scan and show the LLM context that would be sent (diagnostic)
testgenai generate Full pipeline: scan → context → generate → validate
testgenai version Show version
testgenai help Show help
scan
Finds untested methods and prints them. No API calls are made.
bin/testgenai scan
bin/testgenai scan --test-framework minitestcontext
Like scan, but also prints the context snippet that would be sent to the LLM
for each method. Useful for inspecting what the generator will see before
running a full generate.
generate
Runs the full pipeline: scan → build context → call LLM → validate generated tests → report results.
bin/testgenai generate --provider anthropic --model claude-opus-4-7
bin/testgenai generate --provider anthropic --model claude-opus-4-7 --conventionsGenerated tests are injected into existing spec/test files when they exist,
or written to new files mirroring the source path under spec/ (rspec) or
test/ (minitest). If validation fails after injection, the original file is
restored and the generated tests are saved to a per-method fallback file
(e.g. widget_render_spec.rb) for manual review.
The --conventions flag analyzes your existing test suite to detect patterns
(naming conventions, shared contexts, matchers, etc.) and includes them in the
LLM prompt so generated tests match your project's style. The analysis result
is cached in spec/conventions.md (or test/conventions.md) and refreshed
automatically when your test files change.
How scanning works
TestGenAI uses one of two scanners depending on whether SimpleCov is available in your project.
SimpleCov scanner (accurate)
If your Gemfile or gemspec contains simplecov, TestGenAI runs your test
suite with COVERAGE=true to generate coverage/.resultset.json, then uses
that data to find methods where every executable line has zero hits.
This scanner correctly handles partially-tested files — it only reports methods that were never exercised, even if other methods in the same file are tested.
To use the SimpleCov scanner, add SimpleCov to your project and configure it to
start when COVERAGE=true is set:
# Gemfile
gem "simplecov", require: false, group: [:development, :test]# spec/spec_helper.rb or test/test_helper.rb
if ENV["COVERAGE"]
require "simplecov"
SimpleCov.start
endIf coverage/.resultset.json already exists from a previous run, TestGenAI
uses it directly without re-running the test suite.
File-existence scanner (fallback)
If SimpleCov is not detected, TestGenAI falls back to checking whether a corresponding spec/test file exists for each source file. Any source file with no matching test file has all its methods reported as untested.
This scanner cannot detect untested methods in partially-tested files. A file tested only through integration tests or through specs for its subclasses will appear fully untested even though its methods are exercised.
Troubleshooting
"SimpleCov not found in Gemfile or gemspec. Using file-existence scanner."
SimpleCov is not declared in your Gemfile or gemspec. The fallback scanner is
being used. See Setup above to enable the accurate scanner.
The file-existence scanner reports many false positives
Base classes, modules, and shared utilities that are only tested indirectly (through subclass specs or integration tests) will be reported as untested by the file-existence scanner. This is expected — it can only see whether a matching test file exists, not whether code was actually executed. Use the SimpleCov scanner for accurate results.
"Coverage generation failed. Using file-existence scanner."
SimpleCov was found in the Gemfile, but running the test suite with COVERAGE=true did not produce coverage/.resultset.json. Check that:
- SimpleCov is configured to start when
COVERAGE=trueis set (see above) - Your test suite runs successfully with
COVERAGE=true bundle exec rspec(or equivalent for minitest)