Test Strategy
Overview of the test pyramid, coverage goals and best practices for SOCSuite.
Test Pyramid
E2E (Playwright)
─────────────────────
│ Critical workflows │ 10% — Safety-critical
└─────────────────────┘
API Contract Tests
──────────────────────────
│ HTTP + JSON Schema │ 15% — All public endpoints
└─────────────────────────┘
Integration Tests
──────────────────────────
│ Testcontainers + Redis │ 25% — Component interaction
└──────────────────────────┘
Unit Tests (Service + Repository)
──────────────────────────────────────────
│ sqlmock + Testify + Table-Driven Tests │ 50% — Business logic
└─────────────────────────────────────────┘Coverage Goals
| Level | Minimum | Target |
|---|---|---|
| Unit tests (backend) | 80% | 85% |
| Integration tests | 60% critical services | 75% |
| API contract tests | 90% of all endpoints | 100% |
| Frontend (React) | 75% | 80% |
Unit Tests
Technology: Testify + sqlmock + Table-Driven Tests
func TestAlertService_GetByID(t *testing.T) {
tests := []struct {
name string
id string
want *domain.Alert
wantErr error
}{
{"valid id", "uuid-1", &domain.Alert{ID: "uuid-1"}, nil},
{"not found", "unknown", nil, domain.ErrNotFound},
{"empty id", "", nil, domain.ErrValidation},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := svc.GetByID(ctx, tt.id)
assert.ErrorIs(t, err, tt.wantErr)
assert.Equal(t, tt.want, got)
})
}
}go test ./internal/...
go test -cover ./internal/...Integration Tests
Technology: Testcontainers (PostgreSQL 16) — start the container once in TestMain, reuse for all tests. TRUNCATE CASCADE between tests.
:::warn [No SQLite as PostgreSQL substitute] PostgreSQL-specific features (JSONB, full-text search, transactions) are handled differently. :::
//go:build integration
func TestMain(m *testing.M) {
container := startPostgreSQLContainer()
defer container.Terminate(ctx)
applySchema(container.ConnectionString(), "../../02_Initialisation/initdb/")
os.Exit(m.Run())
}go test -tags=integration ./tests/integration/...
go test -tags=integration -race ./tests/integration/...E2E Tests
Technology: Playwright (Chromium, Firefox, WebKit)
:::info [Essential for a SOC platform] A failed incident response is a security risk. :::
Critical scenarios (mandatory):
| Scenario | Description |
|---|---|
| Incident Response | Alert → Case → Playbook → Containment → Closure |
| RBAC Enforcement | Viewer cannot edit a case |
| Multi-User Collaboration | 2 analysts, real-time updates via WebSocket |
| Evidence Chain-of-Custody | SHA256 verified, transfer logged |
| Auth & Session | Login → Token refresh → Logout |
| Error Recovery | DB down, OpenSearch down, agent offline |
pnpm test:e2e
pnpm test:e2e --headed # Visible browser
pnpm test:e2e --reporter=html # HTML reportFrontend Tests
Technology: Vitest + React Testing Library + MSW v2
test("shows alert count", async () => {
render(<AlertDashboard />);
expect(await screen.findByText("5 Alerts")).toBeInTheDocument();
});pnpm test:unit # Unit tests
pnpm test:coverage # Coverage reportQuality Gates
| Phase | Checks | Limit |
|---|---|---|
| pre-commit | Unit ≥80%, gosec HIGH | <3 min |
| pre-merge | go test -race, integration, govulncheck | <5 min |
| post-merge | E2E, OWASP ZAP, trivy | <10 min |
Test Structure
apps/api/
├── internal/
│ ├── service/
│ │ └── *_test.go ← Unit tests (sqlmock)
│ └── repository/
│ └── *_test.go ← Unit tests (sqlmock)
└── tests/
├── integration/ ← Testcontainers (build tag: integration)
├── api/ ← API contract tests
└── testutil/ ← Shared test helpers
apps/web/
└── src/
└── **/*.test.ts(x) ← Vitest unit tests
tests/
└── e2e/ ← Playwright E2EBest Practices
:::info [DO]
- Table-driven tests for validation logic
t.Parallel()for independent unit testsTRUNCATE CASCADEinstead ofDELETEin integration tests- Test names:
TestService_Method_Scenario:::
:::error [DON'T]
- No
time.Sleep()→ usewaitFor/ Testify with timeout - No dependencies between tests
- No SQLite as a PostgreSQL substitute
- No production secrets in tests :::
Further Reading
- Contributing — Git workflow and PR checklist
- Dev Setup — Lint tools and IDE configuration
- Architecture — Clean Architecture and layered model