A Neovim plugin for generating Go test templates using LSP (gopls) to extract function signatures.
- π Automatically generates test templates for Go functions
- π Uses LSP (gopls) to extract function signatures with full type information
- π Falls back to Treesitter or minimal template when gopls is unavailable
- π Generates Goland-compatible test templates with:
- Proper args struct with typed fields
- Table-driven test structure
- Error handling with
assert.ErrorAssertionFunc - Appropriate want fields based on return types
- Neovim 0.8+ (for LSP support)
- gopls (Go language server) - usually installed with Go development environment
- Optional: nvim-treesitter with Go parser for fallback support
Using lazy.nvim (Recommended)
return {
{
"YuminosukeSato/gogentest",
ft = "go",
dependencies = { "neovim/nvim-lspconfig" },
keys = {
{ "<leader>tG", function() require("gogentest").generate() end, desc = "Generate Go Test" },
},
},
}If you're using LazyVim, create a file ~/.config/nvim/lua/plugins/gogentest.lua:
return {
{
"YuminosukeSato/gogentest",
ft = "go",
dependencies = { "neovim/nvim-lspconfig" },
keys = {
{ "<leader>tG", function() require("gogentest").generate() end, desc = "Generate Go Test" },
},
config = function()
-- Optional: Add any custom configuration here
end,
},
-- Ensure gopls is properly configured
{
"neovim/nvim-lspconfig",
opts = {
servers = {
gopls = {
settings = {
gopls = {
analyses = {
unusedparams = true,
},
staticcheck = true,
},
},
},
},
},
},
}Using packer.nvim
use {
"YuminosukeSato/gogentest",
ft = "go",
requires = { "neovim/nvim-lspconfig" },
config = function()
-- Optional keymapping
vim.api.nvim_set_keymap('n', '<leader>tG',
':lua require("gogentest").generate()<CR>',
{ noremap = true, silent = true, desc = "Generate Go Test" })
end,
}- Place your cursor on a Go function you want to test
- Run
:GogentestGenerateor use your configured keybinding (default:<leader>tG) - The plugin will:
- Create a
*_test.gofile if it doesn't exist - Generate a test template with proper types extracted from gopls
- Open the test file with the generated template
- Create a
Given this Go function:
func ProcessData(ctx context.Context, id int, data string) (string, error) {
if id <= 0 {
return "", errors.New("invalid id")
}
if data == "" {
return "", errors.New("empty data")
}
return fmt.Sprintf("processed: %s (id: %d)", data, id), nil
}The plugin generates:
package mypackage_test
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestProcessData(t *testing.T) {
type args struct {
ctx context.Context
id int
data string
}
tests := []struct {
name string
args args
want string
wantErr assert.ErrorAssertionFunc
}{
// TODO add cases
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ProcessData(tt.args.ctx, tt.args.id, tt.args.data)
if !tt.wantErr(t, err, fmt.Sprintf("ProcessData(%v, %v, %v)", tt.args.ctx, tt.args.id, tt.args.data)) {
return
}
assert.Equalf(t, tt.want, got, "ProcessData(%v, %v, %v)", tt.args.ctx, tt.args.id, tt.args.data)
})
}
}You can customize keymappings in your Neovim configuration:
-- In your init.lua or lua/config/keymaps.lua
vim.api.nvim_create_autocmd("FileType", {
pattern = "go",
callback = function()
vim.keymap.set("n", "<leader>gt", function()
require("gogentest").generate()
end, { buffer = true, desc = "Generate Go test" })
end,
})If you prefer to configure keymaps separately in LazyVim:
-- In ~/.config/nvim/lua/config/keymaps.lua
local function map(mode, lhs, rhs, opts)
vim.keymap.set(mode, lhs, rhs, opts)
end
-- Go test generation
map("n", "<leader>tg", function() require("gogentest").generate() end,
{ desc = "Generate Go test" })- LSP First: The plugin tries to get function signature from gopls using
textDocument/signatureHelp - Parse Signature: Extracts function name, parameter names/types, and return types
- Generate Template: Creates a table-driven test with proper type information
- Fallback: If gopls is unavailable, falls back to Treesitter (function name only) or minimal template
Ensure gopls is installed and running:
go install golang.org/x/tools/gopls@latestThen restart Neovim or run :LspRestart.
Make sure your cursor is on or inside a Go function declaration. The plugin looks for function signatures at the cursor position.
Check that:
- gopls is properly configured
- The Go file has no syntax errors
- Your Go module is properly initialized (
go mod init)
If the plugin isn't loading:
- Run
:Lazy syncto ensure it's installed - Check
:Lazyto see if the plugin is loaded - Verify the file type is detected as "go" with
:set ft?
make testmake lintmake formatmake checkMIT License - see LICENSE for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Built with plenary.nvim for testing
- Inspired by various Go test generation tools