Install gotests
developInstall or update gotests using go install. Requires a minimum Go version of 1.22.
$ go install github.com/cweill/gotests/gotests@latestrepository·develop·Indexed 26 days ago
https://github.com/cweill/gotestsA Go test generator that automatically creates table-driven tests from source code. It supports Go generics, custom templates (including testify), and AI-powered test case generation via local Ollama instances. The tool provides a CLI for generating tests for specific functions, exported methods, or entire directories, with options to customize output format, subtest behavior, and comparison libraries like google/go-cmp.
Install or update gotests using go install. Requires a minimum Go version of 1.22.
$ go install github.com/cweill/gotests/gotests@latestgotests supports Go generics (type parameters) introduced in Go 1.18+. It can automatically generate tests for generic functions and methods on generic types by instantiating them with concrete types.
To generate tests for a file containing generic code, use the -all and -w flags:
gotests -all -w yourfile.goUse the -ai flag to generate intelligent test cases (values, edge cases, and error conditions) using a local LLM via Ollama.
Setup Steps:
ollama pull qwen2.5-coder:0.5b or ollama pull llama3.2:latest).gotests with the -ai flag.Privacy Note: Function bodies are sent to the local LLM. Avoid using -ai on code containing sensitive secrets or proprietary algorithms in comments.
Use the -template flag to specify the testify assertion library for the generated test scaffolding.
$ gotests -all -template testify -w calculator.goUse the -only flag followed by the function name to generate a test for a specific function in a file.
$ gotests -only Add -w math.goUse the -all and -exported flags to generate tests for every exported function and method in a directory or package.
$ gotests -all -exported -w .Use the ./... pattern to process the entire directory tree.
$ gotests -all -w ./...You can customize the generated test code using templates. You can specify a template string, a directory of templates, or external parameters.
Template Sources:
-template <string>: Specify custom test code templates (e.g., testify). Can also be set via GOTESTS_TEMPLATE environment variable.-template_dir <path>: Path to a directory containing custom templates. Takes precedence over -template. Can also be set via GOTESTS_TEMPLATE_DIR environment variable.Template Parameters:
-template_params_file <path>: Read external parameters for the template from a JSON file.-template_params <string>: Read external parameters for the template from JSON provided via stdin.The Options struct allows you to fine-tune how tests are generated, including filtering, formatting, and AI integration.
Only: *regexp.Regexp - Include only functions matching this pattern.Exclude: *regexp.Regexp - Exclude functions matching this pattern.Exported: bool - If true, include only exported methods.PrintInputs: bool - Print function parameters in error messages.Subtests: bool - Use Go 1.7 subtests (t.Run).Parallel: bool - Generate tests that run subtests in parallel.Named: bool - Create a map instead of a slice for test cases.UseGoCmp: bool - Use google/go-cmp (cmp.Equal) instead of reflect.DeepEqual.Template: string - Name of a custom template set.TemplateDir: string - Path to a custom template set.TemplateParams: map[string]interface{} - Custom external parameters for templates.TemplateData: [][]byte - Data slice for templates.UseAI: bool - Enable AI-powered test case generation.AIModel: string - The AI model to use.AIEndpoint: string - The AI API endpoint.AIMinCases: int - Minimum number of test cases to generate.AIMaxCases: int - Maximum number of test cases to generate.Importer: func() types.Importer - A custom Go type importer.Available command-line flags for gotests:
| Flag | Description |
|---|---|
-all | generate tests for all functions and methods |
-excl <regexp> | generate tests for functions and methods that don't match the regexp. Takes precedence over -only, -exported, and -all |
-exported | generate tests for exported functions and methods. Takes precedence over -only and -all |
-i | print test inputs in error messages |
-named | switch table tests from using slice to map (with test name for the key) |
-only <regexp> | generate tests for functions and methods that match only. Takes precedence over -all |
-nosubtests | disable subtest generation when >= Go 1.7 |
-parallel | enable parallel subtest generation when >= Go 1.7 |
-w | write output to (test) files instead of stdout |
-template_dir <path> | Path to a directory containing custom test code templates. Takes precedence over -template. Can also be set via GOTESTS_TEMPLATE_DIR |
-template <string> | Specify custom test code templates, e.g. testify. Can also be set via GOTESTS_TEMPLATE |
-template_params_file <path> | read external parameters to template by json with file |
-template_params | read external parameters to template by json with stdin |
-use_go_cmp | use cmp.Equal (google/go-cmp) instead of reflect.DeepEqual |
-ai | generate test cases using AI (requires Ollama) |
-ai-model <string> | AI model to use (default "qwen2.5-coder:0.5b") |
-ai-endpoint <url> | Ollama API endpoint (default "http://localhost:11434") |
-ai-min-cases <int> | minimum number of test cases to generate with AI (default 3) |
-ai-max-cases <int> | maximum number of test cases to generate with AI (default 10) |
-version | print version information and exit |
When generating tests for generic code, gotests uses intelligent defaults to map type constraints to concrete types for instantiation:
| Constraint | Mapped Concrete Type |
|---|---|
any | int |
comparable | string |
Union types (e.g., int64 | float64) | First option (e.g., int64) |
Approximation constraints (e.g., ~int) | Underlying type (e.g., int) |
Run gotests from the command line to generate Go table-driven tests. By default, output is printed to stdout. You can specify paths to files or directories.
$ gotests [options] PATH ...