一键导入
testing-strategy
Test patterns, mocking strategies, and organization best practices
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Test patterns, mocking strategies, and organization best practices
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
ZGO API development standards including pagination, error handling, and RESTful design
Standardized error format, error code clusters, and API client usage for consistent error handling.
Specifications for Zustand stores, React Query hooks, and the Service-Hook-Type pattern with optimistic updates.
Strict rules for environment variable management using Zod validation and src/config/env.ts.
Guidelines for managing internationalization (i18n) in the project using next-intl and unified translation patterns.
High-level overview of project structure, mock API architecture, and authentication flow.
| name | testing-strategy |
| description | Test patterns, mocking strategies, and organization best practices |
| version | 1.0.0 |
| category | development |
| tags | ["testing","unit-test","integration-test","mock","testify"] |
| author | ZGO Team |
| updated | "2026-01-24T00:00:00.000Z" |
This skill defines the testing standards and strategies for the ZGO project. It ensures that code is reliable, maintainable, and verifiable through various testing tiers.
testify/assert and testify/mock*_test.go).tests/integration/ or package-specific integration subdirectories.tests/api/ or tests/e2e/.Table-driven tests are the standard in Go. They allow testing multiple scenarios with minimal boilerplate.
func TestCalculateTotal(t *testing.T) {
tests := []struct {
name string
items []float64
tax float64
expected float64
}{
{"empty items", []float64{}, 0.1, 0},
{"single item", []float64{100}, 0.1, 110},
{"multiple items", []float64{10, 20, 30}, 0.05, 63},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := CalculateTotal(tt.items, tt.tax)
assert.Equal(t, tt.expected, result)
})
}
}
{original_file}_test.gopackage user) or package user_test for external black-box testing.We use github.com/stretchr/testify/mock for dependency injection.
Mocks should implement the interface of the dependency they replace.
// MockRepository is an autogenerated mock type for the Repository type
type MockRepository struct {
mock.Mock
}
func (m *MockRepository) GetByID(ctx context.Context, id uint) (*domain.User, error) {
args := m.Called(ctx, id)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*domain.User), args.Error(1)
}
func TestService_GetByID(t *testing.T) {
mockRepo := new(MockRepository)
service := NewService(mockRepo)
ctx := context.Background()
expectedUser := &domain.User{ID: 1, Username: "stark"}
// Set expectation
mockRepo.On("GetByID", ctx, uint(1)).Return(expectedUser, nil)
// Execute
user, err := service.GetByID(ctx, 1)
// Assert
assert.NoError(t, err)
assert.Equal(t, expectedUser, user)
// Verify all expectations were met
mockRepo.AssertExpectations(t)
}
Integration tests verify that components work together.
Use a test database (SQLite in-memory or a dedicated PostgreSQL container).
func TestRepository_Create(t *testing.T) {
db := setupTestDB() // Helper to get a clean DB
repo := NewRepository(db)
user := &domain.User{Username: "testuser", Email: "test@example.com"}
err := repo.Create(context.Background(), user)
assert.NoError(t, err)
assert.NotZero(t, user.ID)
}
Use net/http/httptest to test handlers without starting a real server.
func TestHandler_Create(t *testing.T) {
gin.SetMode(gin.TestMode)
mockService := new(MockService)
handler := NewHandler(mockService)
r := gin.Default()
r.POST("/users", handler.Create)
body := `{"username": "test", "email": "test@example.com"}`
req, _ := http.NewRequest("POST", "/users", strings.NewReader(body))
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusCreated, w.Code)
}
testify: Use assert for non-terminal failures and require for failures that should stop the test immediately.Test. Sub-tests (in t.Run) should have descriptive names.# Run all tests
make test
# Run tests in a specific module
go test ./internal/modules/user/...
# Run with coverage
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
We use mockery for automatic mock generation.
# Generate mocks for all interfaces in a directory
mockery --all --dir=internal/modules/user
_test.go.AssertExpectations).module-creation: Structure of test files in modules.coding-standards: General code quality that affects testability.api-development: Testing API responses and pagination.Version: 1.0.0
Last Updated: 2026-01-24
Maintainer: ZGO Team