ワンクリックで
test-writing
Guidelines for writing effective unit tests, integration tests, and test coverage. Use when creating tests.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Guidelines for writing effective unit tests, integration tests, and test coverage. Use when creating tests.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
Python 3.11+ key decisions and patterns. Use when writing Python code.
Rust 2021 edition coding conventions. Use when writing or reviewing Rust code.
TypeScript 5.x / JavaScript ES2022+ coding conventions. Use when writing or reviewing TypeScript/JavaScript code.
Repro-first bug investigation process with root-cause validation and fix planning.
Keep user-facing documentation aligned with current behavior.
Pre-release validation checklist with go/no-go recommendation.
| name | test-writing |
| description | Guidelines for writing effective unit tests, integration tests, and test coverage. Use when creating tests. |
Follow these guidelines when writing tests for this project.
name_test.go (next to source file)TestFunctionName or TestScenariotestHelperName (unexported)t.Run("description", func(t *testing.T) {...})func TestProcessUser(t *testing.T) {
// Arrange - set up test data and mocks
user := &User{ID: 1, Name: "Alice"}
mockDB.On("Get", 1).Return(user, nil)
// Act - call the function being tested
result, err := ProcessUser(1)
// Assert - verify the result
require.NoError(t, err)
require.Equal(t, "Alice", result.Name)
mockDB.AssertExpectations(t)
}
Use for multiple input/output combinations:
func TestValidateEmail(t *testing.T) {
tests := []struct {
name string
email string
wantErr bool
}{
{"valid", "test@example.com", false},
{"invalid no @", "testexample.com", true},
{"invalid no domain", "test@", true},
{"empty", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateEmail(tt.email)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
Use testify/require for assertions that must pass (fail fast):
require.NoError(t, err) // stops test on failure
require.Equal(t, expected, actual)
require.NotNil(t, obj)
require.Len(t, slice, 3)
require.Contains(t, str, "substring")
Use testify/assert for non-critical checks:
assert.NoError(t, err) // continues on failure
assert.True(t, condition, "optional message")
type MockDB struct {
mock.Mock
}
func (m *MockDB) Get(id int) (*User, error) {
args := m.Called(id)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*User), args.Error(1)
}
func TestGetUser(t *testing.T) {
mockDB := new(MockDB)
service := NewUserService(mockDB)
mockDB.On("Get", 1).Return(&User{ID: 1, Name: "Alice"}, nil)
user, err := service.GetUser(1)
require.NoError(t, err)
require.Equal(t, "Alice", user.Name)
mockDB.AssertExpectations(t)
}
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
func createTestUser(t *testing.T) *User {
return &User{
ID: uuid.New(),
Name: "Test User " + t.Name(),
}
}
TestValidateEmail_RejectsInvalidFormatfunc TestHealthHandler(t *testing.T) {
req := httptest.NewRequest("GET", "/health", nil)
w := httptest.NewRecorder()
HealthHandler(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Contains(t, w.Body.String(), "healthy")
}
func TestDivide_ByZero(t *testing.T) {
_, err := Divide(10, 0)
require.Error(t, err)
require.Contains(t, err.Error(), "division by zero")
}
go test ./... # all tests
go test -v ./... # verbose
go test -run TestName # specific test
go test -cover ./... # with coverage
go test -race ./... # race detector