What problem does it solve?
This Skill guides Go developers through best practices for Test-Driven Development, ensuring high-quality, maintainable, and well-tested Go code. It standardizes testing approaches, reducing common errors and improving code reliability.
Core Features & Use Cases
- Table-Driven Tests: Provides a structured and efficient approach for testing multiple inputs and scenarios.
- Testify Assertions: Guides on using
require for critical checks (stopping on failure) and assert for continued test execution.
- Mocking External Dependencies: Best practices for isolating unit tests from external services like databases or network calls.
- Use Case: A developer is implementing a new Go service. This skill provides immediate guidance on how to structure their tests, what assertion libraries to use, and how to mock dependencies, ensuring they follow TDD principles from the start and produce high-coverage, reliable code.
Quick Start
Example of a table-driven test in Go
func TestValidation(t *testing.T) {
tests := []struct {
name string
input string
want bool
wantErr error
}{
{name: "valid input", input: "test", want: true, wantErr: nil},
{name: "empty input", input: "", want: false, wantErr: ErrEmptyInput},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Validate(tt.input)
if tt.wantErr != nil {
require.ErrorIs(t, err, tt.wantErr)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}