Testing
Ce contenu n’est pas encore disponible dans votre langue.
Overview
Section titled “Overview”Testing ensures your application works correctly and prevents regressions.
Unit Testing
Section titled “Unit Testing”Testing Services
Section titled “Testing Services”func TestUserService_Create(t *testing.T) { service := &UserService{ users: make(map[string]*User), }
if err != nil { t.Fatalf("unexpected error: %v", err) }
}}Testing with Mocks
Section titled “Testing with Mocks”type MockDB struct { users map[string]*User}
func (m *MockDB) Create(user *User) error { m.users[user.ID] = user return nil}
func TestUserService_WithMock(t *testing.T) { mockDB := &MockDB{users: make(map[string]*User)} service := &UserService{db: mockDB}
if err != nil { t.Fatal(err) }
if len(mockDB.users) != 1 { t.Error("expected 1 user in mock") }}Integration Testing
Section titled “Integration Testing”Testing with Real Dependencies
Section titled “Testing with Real Dependencies”func TestIntegration(t *testing.T) { // Setup test database db, err := sql.Open("sqlite3", ":memory:") if err != nil { t.Fatal(err) } defer db.Close()
// Create schema _, err = db.Exec(`CREATE TABLE users (...)`) if err != nil { t.Fatal(err) }
// Test service service := &UserService{db: db} if err != nil { t.Fatal(err) }
// Verify in database var count int db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count) if count != 1 { t.Errorf("expected 1 user, got %d", count) }}Frontend Testing
Section titled “Frontend Testing”JavaScript Unit Tests
Section titled “JavaScript Unit Tests”// Using Vitestimport { describe, it, expect } from 'vitest'import { formatDate } from './utils'
describe('formatDate', () => { it('formats date correctly', () => { const date = new Date('2024-01-01') expect(formatDate(date)).toBe('2024-01-01') })})Testing Bindings
Section titled “Testing Bindings”import { vi } from 'vitest'import { GetUser } from './bindings/changeme/userservice'
// Mock the bindingvi.mock('./bindings/changeme/userservice', () => ({ GetUser: vi.fn()}))
describe('User Component', () => { it('loads user data', async () => {
// Test your component const user = await GetUser(1) expect(user.name).toBe('John') })})Best Practices
Section titled “Best Practices”- Write tests before fixing bugs
- Test edge cases
- Use table-driven tests
- Mock external dependencies
- Test error handling
- Keep tests fast
❌ Don’t
Section titled “❌ Don’t”- Don’t skip error cases
- Don’t test implementation details
- Don’t write flaky tests
- Don’t ignore test failures
- Don’t skip integration tests
Running Tests
Section titled “Running Tests”# Run Go testsgo test ./...
# Run with coveragego test -cover ./...
# Run specific testgo test -run TestUserService
# Run frontend testscd frontend && npm test
# Run with watch modecd frontend && npm test -- --watchNext Steps
Section titled “Next Steps”- End-to-End Testing - Test complete user flows
- Best Practices - Learn best practices