mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 16:30:01 +08:00
Add complete test scaffolding support with --test flag for API projects (#5176)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kevwan <1918356+kevwan@users.noreply.github.com>
This commit is contained in:
120
tools/goctl/api/gogen/integration_test.tpl
Normal file
120
tools/goctl/api/gogen/integration_test.tpl
Normal file
@@ -0,0 +1,120 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl {{.version}}
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"{{.projectPkg}}/internal/config"
|
||||
"{{.projectPkg}}/internal/handler"
|
||||
"{{.projectPkg}}/internal/svc"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// TODO: Add setup/teardown logic here if needed
|
||||
m.Run()
|
||||
}
|
||||
|
||||
func TestServerIntegration(t *testing.T) {
|
||||
// Create test server
|
||||
c := config.Config{
|
||||
RestConf: rest.RestConf{
|
||||
Host: "127.0.0.1",
|
||||
Port: 0, // Use random available port
|
||||
},
|
||||
}
|
||||
|
||||
server := rest.MustNewServer(c.RestConf)
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
// Start server in background
|
||||
go func() {
|
||||
server.Start()
|
||||
}()
|
||||
|
||||
// Wait for server to start
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
method string
|
||||
path string
|
||||
body string
|
||||
expectedStatus int
|
||||
setup func()
|
||||
}{
|
||||
{
|
||||
name: "health check",
|
||||
method: "GET",
|
||||
path: "/health",
|
||||
expectedStatus: http.StatusNotFound, // Adjust based on actual routes
|
||||
setup: func() {},
|
||||
},
|
||||
{{if .hasRoutes}}{{range .routes}}{
|
||||
name: "{{.Method}} {{.Path}}",
|
||||
method: "{{.Method}}",
|
||||
path: "{{.Path}}",
|
||||
expectedStatus: http.StatusOK, // TODO: Adjust expected status
|
||||
setup: func() {
|
||||
// TODO: Add setup logic for this endpoint
|
||||
},
|
||||
},
|
||||
{{end}}{{end}}{
|
||||
name: "not found route",
|
||||
method: "GET",
|
||||
path: "/nonexistent",
|
||||
expectedStatus: http.StatusNotFound,
|
||||
setup: func() {},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.setup()
|
||||
|
||||
req, err := http.NewRequest(tt.method, tt.path, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
server.ServeHTTP(rr, req)
|
||||
|
||||
assert.Equal(t, tt.expectedStatus, rr.Code)
|
||||
|
||||
// TODO: Add response body assertions
|
||||
t.Logf("Response: %s", rr.Body.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerLifecycle(t *testing.T) {
|
||||
c := config.Config{
|
||||
RestConf: rest.RestConf{
|
||||
Host: "127.0.0.1",
|
||||
Port: 0,
|
||||
},
|
||||
}
|
||||
|
||||
server := rest.MustNewServer(c.RestConf)
|
||||
|
||||
// Test server can start and stop without errors
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
// In a real integration test, you might start the server in a goroutine
|
||||
// and test actual HTTP requests, but for scaffolding we keep it simple
|
||||
server.Stop()
|
||||
|
||||
// TODO: Add more lifecycle tests as needed
|
||||
assert.True(t, true, "Server lifecycle test passed")
|
||||
}
|
||||
Reference in New Issue
Block a user