Loading Go Test Generator...
How to Generate Go Tests - Step by Step Guide
Note: The page writes the test skeleton from your function signatures; the test cases are yours to fill in. Functions it cannot turn into compiling code, such as generics constrained by an interface with methods, are listed in a note instead.
Paste your Go file
Copy and paste your Go functions into the input editor, starting with the package line. Every top-level function and method gets a test written the way the Go testing docs describe:
Example: Input Go Function
Here's a simple Go function you want to test:
package calculator // Add returns the sum of two integers func Add(a, b int) int { return a + b }
Check the generated tests
The output follows the pattern from the Go testing tutorial, written out for each function:
Example: Generated Test Code
This is what the page writes for the Add function from Step 1:
package calculator
import (
"reflect"
"testing"
)
func TestAdd(t *testing.T) {
type args struct {
a int
b int
}
tests := []struct {
name string
args args
want int
}{
// TODO: Add test cases, for example:
// {name: "basic", args: args{a: 0, b: 0}, want: 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Add(tt.args.a, tt.args.b)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Add() = %v, want %v", got, tt.want)
}
})
}
}The table is empty on purpose: add your own rows, like the commented example, before you rely on the test.
Copy or download and fill in cases
Get your test code ready to use in your Go module:
Example: Running Your Tests
After adding the row {name: "positive numbers", args: args{a: 2, b: 3}, want: 5}, save as calculator_test.go and run:
$ go test -v === RUN TestAdd === RUN TestAdd/positive_numbers --- PASS: TestAdd (0.00s) --- PASS: TestAdd/positive_numbers (0.00s) PASS ok calculator 0.002s
Want to tidy the source file first? The Go formatter lays it out the way gofmt does.
Frequently Asked Questions
What does a generated test look like?
Each function gets a table-driven test in the gotests style: an args struct with one field per parameter, a want field per result, a wantErr flag when the last result is an error, and a loop that calls the function inside t.Run. Results are compared with reflect.DeepEqual, so slices, maps and structs work too. The table starts empty with one example row in a comment, so the file passes until you fill in real cases.
Will the test file compile without changes?
For ordinary Go code it should. The package name is taken from your package line, only the imports the test actually uses are written, methods are called on a receiver of the right type, variadic arguments are passed with "...", and generic functions get a concrete type picked from their constraint (int for any, comparable and cmp.Ordered, or the first type of a union like ~int | ~float64). Anything the page cannot turn into code that compiles is left out and listed in a note at the top of the file instead.
Which functions are left out, and why?
main and init are skipped because Go runs them for you, and functions that already look like tests (TestX, BenchmarkX, ExampleX) are skipped too. A generic function is left out when its constraint has methods, such as fmt.Stringer, or depends on another type parameter, because no safe concrete type can be picked. Functions that use cgo types or names from a dot import are also skipped. Each one is named in a note so you know what is missing.
How are methods tested?
The test declares a zero value receiver, for example var r User, and calls the method on it. That works for value and pointer receivers alike, because Go takes the address of r for you. For generic types such as Stack[T] the receiver is instantiated, for example Stack[int]. Most real methods need some state, so set the receiver fields at the TODO comment before you add cases.
Does it check my Go code for mistakes?
Partly. It reads the package line, imports and every top-level declaration, so unclosed brackets or strings, a missing comma, a broken function signature or a statement outside a function are reported with the line and column. It does not type-check what is inside function bodies, so run go vet on your own code as well.
How do I run the tests?
Save the output as <package>_test.go next to your source file, for example calculator_test.go, add a few rows to each table, and run go test -v ./... from the module root. Rows you leave as comments are not run, so an untouched skeleton simply passes.
Is my code sent anywhere?
No. The tests are generated by JavaScript in your browser tab; your code is not uploaded to our server and nothing is kept, so reloading the page starts with an empty editor.
Related Tools
Go Formatter
Format and beautify Go code with gofmt-style indentation
Go Validator
Validate Go syntax and catch common errors
Go Minifier
Minify Go code by removing comments and whitespace
Go Beautifier
Beautify Go code for better readability
Rust Test Generator
Generate unit test templates for Rust functions
Cargo.toml Generator
Generate Cargo.toml files for Rust projects with dependencies