Test-Strategie
Überblick über die Test-Pyramide, Coverage-Ziele und Best Practices für SOCSuite.
Test-Pyramide
E2E (Playwright)
─────────────────────
│ Kritische Workflows │ 10% — Safety-Critical
└─────────────────────┘
API Contract Tests
──────────────────────────
│ HTTP + JSON Schema │ 15% — Alle Public Endpoints
└─────────────────────────┘
Integration Tests
──────────────────────────
│ Testcontainers + Redis │ 25% — Komponenten-Interaktion
└──────────────────────────┘
Unit Tests (Service + Repository)
──────────────────────────────────────────
│ sqlmock + Testify + Table-Driven Tests │ 50% — Business-Logik
└─────────────────────────────────────────┘Coverage-Ziele
| Ebene | Minimum | Ziel |
|---|---|---|
| Unit Tests (Backend) | 80% | 85% |
| Integration Tests | 60% kritische Services | 75% |
| API Contract Tests | 90% aller Endpoints | 100% |
| Frontend (React) | 75% | 80% |
Unit Tests
Technik: 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
Technik: Testcontainers (PostgreSQL 16) — Container einmal in TestMain starten, für alle Tests wiederverwenden. TRUNCATE CASCADE zwischen Tests.
:::warn [Kein SQLite als PostgreSQL-Ersatz] PostgreSQL-spezifische Features (JSONB, Full-Text-Search, Transaktionen) werden unterschiedlich behandelt. :::
//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
Technik: Playwright (Chromium, Firefox, WebKit)
:::info [Für eine SOC-Plattform unverzichtbar] Fehlgeschlagene Incident-Response ist ein Sicherheitsrisiko. :::
Kritische Szenarien (Pflicht):
| Szenario | Beschreibung |
|---|---|
| Incident Response | Alert → Case → Playbook → Containment → Closure |
| RBAC Enforcement | Viewer kann Case nicht editieren |
| Multi-User Collaboration | 2 Analysten, Realtime-Updates via WebSocket |
| Evidence Chain-of-Custody | SHA256 verified, Transfer geloggt |
| Auth & Session | Login → Token-Refresh → Logout |
| Error-Recovery | DB-Down, OpenSearch-Down, Agent-Offline |
pnpm test:e2e
pnpm test:e2e --headed # Sichtbarer Browser
pnpm test:e2e --reporter=html # HTML-ReportFrontend Tests
Technik: 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-Struktur
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 für Validierungslogik
t.Parallel()für unabhängige Unit-TestsTRUNCATE CASCADEstattDELETEin Integration-Tests- Test-Namen:
TestService_Method_Scenario:::
:::error [DON'T]
- Kein
time.Sleep()→waitFor/ Testify mit Timeout - Keine Abhängigkeiten zwischen Tests
- Kein SQLite als PostgreSQL-Ersatz
- Keine Production-Secrets in Tests :::
Weiterführend
- Contributing — Git-Workflow und PR-Checkliste
- Dev-Setup — Lint-Tools und IDE-Konfiguration
- Architektur — Clean Architecture und Schichtenmodell