AgentSkillsCN

testing

测试模式包括表格驱动测试、Mock 设置,以及覆盖率要求。适用于编写测试用例,或搭建测试基础设施时使用。

SKILL.md
--- frontmatter
name: testing
description: Testing patterns including table-driven tests, mock setup, and coverage requirements. Use when writing tests or setting up test infrastructure.
userInvokable: true

Testing

References: Examples

Rules

RuleValue
Unit testsTestFunctionName
Integration testsTestIntegration*
Coverage85% minimum
Toolstestify/assert, gomock

Mock Generation

Example

Add at top of file with interfaces:

go
//go:generate mockgen -source=service.go -destination=service_mock.go -package=<pkg>

Run with:

bash
make generate

Table-Driven Tests

Example

Use for testing multiple scenarios:

go
func TestFunction(t *testing.T) {
    tests := []struct {
        name     string
        input    string
        expected string
        wantErr  bool
    }{
        {name: "valid input", input: "foo", expected: "bar"},
        {name: "empty input", input: "", wantErr: true},
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            // test logic
        })
    }
}

Mock Setup

Example

go
func TestService(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()

    mockRepo := mocks.NewMockRepository(ctrl)
    mockRepo.EXPECT().
        FindByID(gomock.Any(), "id").
        Return(&Entity{}, nil)

    svc := NewService(mockRepo)
    // test...
}

Assertions

Example

Use testify/assert:

go
assert.NoError(t, err)
assert.Equal(t, expected, actual)
assert.Nil(t, result)
assert.NotNil(t, result)
assert.True(t, condition)
assert.Contains(t, slice, element)