gotests

repository·develop·Indexed 26 days ago

https://github.com/cweill/gotests

A 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.

Tokens
2.4K
Snippets
8
Records
19
Agent score
87%

What's inside gotests

  1. Use Go Generics Support in gotests

    develop

    gotests 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.go
  2. Generate tests using AI with Ollama

    develop

    Use the -ai flag to generate intelligent test cases (values, edge cases, and error conditions) using a local LLM via Ollama.

    Setup Steps:

    1. Install Ollama.
    2. Pull a model (e.g., ollama pull qwen2.5-coder:0.5b or ollama pull llama3.2:latest).
    3. Run 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.

  3. Use custom templates for test generation

    develop

    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.
  4. Configure test generation with Options

    develop

    The Options struct allows you to fine-tune how tests are generated, including filtering, formatting, and AI integration.

    Filtering

    • 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.

    Formatting and Output

    • 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.

    Templates

    • 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.

    AI-Powered Generation

    • 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.

    Advanced

    • Importer: func() types.Importer - A custom Go type importer.
  5. Reference: gotests CLI options

    develop

    Available command-line flags for gotests:

    FlagDescription
    -allgenerate 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
    -exportedgenerate tests for exported functions and methods. Takes precedence over -only and -all
    -iprint test inputs in error messages
    -namedswitch 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
    -nosubtestsdisable subtest generation when >= Go 1.7
    -parallelenable parallel subtest generation when >= Go 1.7
    -wwrite 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_paramsread external parameters to template by json with stdin
    -use_go_cmpuse cmp.Equal (google/go-cmp) instead of reflect.DeepEqual
    -aigenerate 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)
    -versionprint version information and exit
  6. Understand Type Constraint Mapping for Generics

    develop

    When generating tests for generic code, gotests uses intelligent defaults to map type constraints to concrete types for instantiation:

    ConstraintMapped Concrete Type
    anyint
    comparablestring
    Union types (e.g., int64 | float64)First option (e.g., int64)
    Approximation constraints (e.g., ~int)Underlying type (e.g., int)