This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
gotests is a Go commandline tool that automatically generates table-driven tests based on function and method signatures. It parses Go source files and generates test files with proper test scaffolding, including automatic import resolution.
# Install the tool
go get -u github.com/cweill/gotests/...
# Build the binary
go build -o gotests ./gotests
# Run all tests
go test -v ./...
# Run tests for a specific package
go test -v ./gotests/process
# Run tests with coverage (excluding certain packages)
export PKGS=$(go list ./... | grep -vE "(gotests/gotests|.*data|templates)" | tr -s '\n' ',' | sed 's/.{1}$//')
go test -v -covermode=count -coverpkg=$PKGS -coverprofile=coverage.cov
# Run a single test
go test -v -run TestSpecificTest ./path/to/packageNote: Tests may fail when run with the -race flag (see comments in codebase).
- Input Processing (
internal/input): Resolves file paths from command-line arguments - Parsing (
internal/goparser): Parses Go source files into AST and extracts function signatures - Model Generation (
internal/models): Converts parsed data into domain models (Function, Receiver, Field, etc.) - Template Rendering (
internal/render): Uses Go templates to generate test code - Output Generation (
internal/output): Combines templates with parsed data and manages imports
gotests.go: Main library entry point
GenerateTests(): Orchestrates the entire test generation pipelineparallelize(): Generates tests for multiple files concurrently using goroutinestestableFuncs(): Filters functions based on options (exported, regex patterns, existing tests)
internal/models/models.go: Core data structures
Function: Represents a function/method with parameters, results, and receiverReceiver: Represents method receivers with their fieldsField: Represents function parameters, results, or receiver fieldsExpression: Type information including whether it's a pointer, variadic, or writer
internal/goparser/goparser.go: AST parsing
- Parses source files into
ast.Fileobjects - Uses
go/typespackage for type checking and resolution - Extracts function signatures, receivers, parameters, and return types
internal/render/render.go: Template system
- Default templates in
internal/render/templates/ - Custom templates supported via
-template(named sets like "testify") or-template_dir(custom directory) - Templates can be embedded as bindata or loaded from filesystem
- Template functions:
Field,Receiver,Param,Want,Gotfor generating variable names
gotests/process/process.go: Command-line processing
- Handles file I/O and error reporting
- Converts CLI options to library options
- Manages writing output to stdout or files with
-wflag
The tool uses Go text templates to generate test code. There are two built-in template sets:
test: Default table-driven teststestify: Tests using the testify assertion library
Templates can be customized via:
-template_dir: Directory with custom templates (takes precedence)-template: Named template set fromtemplates/directory- Environment variables:
GOTESTS_TEMPLATE_DIR,GOTESTS_TEMPLATE
Functions are filtered in this precedence order:
-excl: Exclude functions matching regex (highest precedence)-exported: Only exported functions-only: Only functions matching regex-all: All functions (default is none)
Existing test functions are automatically excluded to avoid duplication.
- The project uses Go modules (
go.mod) - Minimum supported Go version: 1.6
- The codebase uses concurrent test generation via goroutines for performance
- Tests are in
testdata/directories with golden file comparisons intestdata/goldens/ - The
templates/directory contains built-in template sets - Bindata is used to embed templates in the binary (via
internal/render/bindata/) - Always use scripts/regenerate-goldens.sh to generate the goldens for tests.