Write comprehensive, idiomatic tests following best practices and project conventions. Use this when writing unit tests, integration tests, or test fixtures...
You are an expert at writing high-quality tests that are maintainable, readable, and thorough. Use this skill when:
func TestFunctionName(t *testing.T) {
// Arrange: Set up test data and dependencies
input := "test data"
expected := "expected result"
// Act: Execute the function being tested
result, err := FunctionName(input)
// Assert: Verify the results
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != expected {
t.Errorf("got %v, want %v", result, expected)
}
}
func TestFunctionName(t *testing.T) {
tests := []struct {
name string
input string
expected string
wantErr bool
}{
{
name: "valid input",
input: "test",
expected: "result",
wantErr: false,
},
{
name: "invalid input",
input: "",
expected: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := FunctionName(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
return
}
if result != tt.expected {
t.Errorf("got %v, want %v", result, tt.expected)
}
})
}
}
type MockService struct {
DoWorkFunc func(input string) (string, error)
}
func (m *MockService) DoWork(input string) (string, error) {
if m.DoWorkFunc != nil {
return m.DoWorkFunc(input)
}
return "", nil
}
func setupTest(t *testing.T) (*Component, func()) {
// Setup
component := NewComponent()
// Return cleanup function
return component, func() {
// Cleanup
component.Close()
}
}
func TestWithFixture(t *testing.T) {
component, cleanup := setupTest(t)
defer cleanup()
// Use component in test
}
describe('FunctionName', () => {
it('should handle valid input', () => {
// Arrange
const input = 'test';
const expected = 'result';
// Act
const result = functionName(input);
// Assert
expect(result).toBe(expected);
});
it('should throw on invalid input', () => {
expect(() => functionName('')).toThrow();
});
});
jest.mock('../service');
describe('Component', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should call service', async () => {
const mockService = jest.fn().mockResolvedValue('result');
const component = new Component(mockService);
await component.doWork();
expect(mockService).toHaveBeenCalledWith(expect.any(String));
});
});
file.go → file_test.go)package_test for integration tests, same package for unit testsfunc TestHandlesNilInput(t *testing.T) {
_, err := Process(nil)
if err == nil {
t.Error("expected error for nil input")
}
}
func TestConcurrentAccess(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// Concurrent operations
}()
}
wg.Wait()
}
func TestContextCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately
err := Operation(ctx)
if err != context.Canceled {
t.Errorf("expected Canceled error, got %v", err)
}
}
When asked to write tests:
Understand the Code
Plan Test Cases
Write Tests
Verify Coverage
go test -vgo test -covergo test -coverprofile=coverage.outReview and Refactor
Check the existing test files to identify:
Test_, test, should_)When you finish writing tests, invoke the Skill tool with an empty skill name to clear this context.