Generate idiomatic tests for Go packages and handlers in the Meshery project.
Generate idiomatic tests for Go packages and handlers in the Meshery project.
Invoke this skill with a target file or package path:
/gen-test server/handlers/provider_handler.go/gen-test mesheryctl/internal/cli/root/system/start.go*_test.go files) to match the project's testing style.t.Run subtestsTest<FunctionName> or Test<Type>_<Method>testify/assert or testify/require if already used in the package; otherwise use standard libraryhttptest.NewRecorder() and httptest.NewRequest()<source_file>_test.go (or append to existing test file if one exists)For handlers (server/handlers/):
For models (server/models/):
For mesheryctl commands (mesheryctl/):
func TestHandlerName(t *testing.T) {
tests := []struct {
name string
method string
path string
body string
expectedStatus int
}{
{
name: "valid request",
method: http.MethodGet,
path: "/api/resource",
expectedStatus: http.StatusOK,
},
{
name: "missing required field",
method: http.MethodPost,
path: "/api/resource",
body: `{}`,
expectedStatus: http.StatusBadRequest,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(tt.method, tt.path, strings.NewReader(tt.body))
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != tt.expectedStatus {
t.Errorf("expected status %d, got %d", tt.expectedStatus, rec.Code)
}
})
}
}