Testing Quick Start Guide
October 22, 2025 ยท View on GitHub
This guide helps you get started with the Netcap test suite.
๐ What Was Created
The test infrastructure includes:
Documentation
docs/TEST_PLAN.md- Comprehensive 16-week test plandocs/TEST_PLAN_SUMMARY.md- Executive summarydocs/TESTING_QUICKSTART.md- This filetests/README.md- Test suite documentation
Infrastructure
tests/- Test directory structurefixtures/- Test data (PCAPs, golden files, configs, databases)integration/- Integration testsregression/- Regression testse2e/- End-to-end testsbenchmarks/- Performance testshelpers/- Test utilities
Utilities
helpers/fixtures.go- Fixture loading and golden file managementhelpers/pcap_generator.go- Synthetic PCAP generationtests/TEMPLATE_test.go- Test template with examplesMakefile.test- Test automation targetsscripts/verify-golden-files.sh- Golden file verification
Examples
tests/integration/example_integration_test.go- Integration test exampletests/regression/example_regression_test.go- Regression test example
๐ Quick Start
1. Run Existing Tests
# Run all unit tests
go test ./...
# Run with coverage
go test -cover ./...
# Run specific package
go test -v ./collector/
# Run with race detector
go test -race ./...
2. Using the Makefile
# View all test targets
make -f Makefile.test test-help
# Run unit tests
make -f Makefile.test test-unit
# Run with coverage report
make -f Makefile.test test-coverage-html
# Opens coverage.html in browser
# Run integration tests
make -f Makefile.test test-integration
# Run regression tests
make -f Makefile.test test-regression
# Run benchmarks
make -f Makefile.test test-bench
3. Writing Your First Test
Option A: Use the Template
# Copy the template
cp tests/TEMPLATE_test.go decoder/packet/myprotocol_test.go
# Edit and implement your test
vim decoder/packet/myprotocol_test.go
# Run your test
go test -v ./decoder/packet/ -run TestMyProtocol
Option B: Start from Scratch
package mypackage_test
import "testing"
func TestMyFeature(t *testing.T) {
// Arrange
input := "test"
// Act
result := MyFunction(input)
// Assert
if result != "expected" {
t.Errorf("got %v, want %v", result, "expected")
}
}
๐ Current Status
Coverage by Package (as of Oct 2025)
| Package | Coverage | Status |
|---|---|---|
| logger | 89.5% | โ Excellent |
| delimited | 69.0% | โ Good |
| reassembly | 68.9% | โ ๏ธ 4 failing tests |
| encoder | 64.0% | โ Good |
| credentials | 52.8% | โ ๏ธ Medium |
| label/manager | 49.3% | โ ๏ธ Medium |
| io | 19.7% | โ Low |
| Most packages | 0-5% | โ Critical |
Immediate Priorities
-
Fix Build Errors (Week 1)
- Fix DBS package unused variables
- Fix 4 failing reassembly tests
- Resolve DPI build issues
- Fix resolver test data issues
-
Core Package Tests (Weeks 2-4)
- Collector: 85% coverage
- Types: 80% coverage
- Decoder/packet: 75% coverage
- I/O: 85% coverage
๐ง Common Tasks
Add Test for New Decoder
# 1. Create test file
cat > decoder/packet/mynewprotocol_test.go << 'EOF'
package packet_test
import (
"testing"
"github.com/dreadl0ck/netcap/decoder/packet"
)
func TestMyNewProtocol_Decode(t *testing.T) {
// Your test here
}
EOF
# 2. Add test PCAP
cp mynewprotocol.pcap tests/fixtures/pcaps/protocols/
# 3. Run test
go test -v ./decoder/packet/ -run TestMyNewProtocol
Create Integration Test
# 1. Create test file
cat > tests/integration/myfeature_test.go << 'EOF'
// +build integration
package integration
import "testing"
func TestIntegration_MyFeature(t *testing.T) {
// Multi-component test
}
EOF
# 2. Run integration tests
go test -tags=integration -v ./tests/integration/
Add Regression Test with Golden File
# 1. Process PCAP to generate output
go run cmd/*.go capture -r test.pcap -out /tmp/test-output
# 2. Save as golden file
mkdir -p tests/fixtures/golden/csv/my_scenario/
cp /tmp/test-output/HTTP.csv tests/fixtures/golden/csv/my_scenario/
# 3. Create regression test
cat > tests/regression/mytest_test.go << 'EOF'
// +build regression
package regression
import (
"testing"
"github.com/dreadl0ck/netcap/helpers"
)
func TestRegression_MyScenario(t *testing.T) {
loader := helpers.NewFixtureLoader(helpers.TestDataPath())
// Generate output
// var got []byte
// Compare with golden
want, err := loader.LoadGoldenFile("HTTP", "csv", "my_scenario")
if err != nil {
t.Skip("Golden file not found:", err)
}
helpers.CompareCSV(t, got, want)
}
EOF
# 4. Run regression test
go test -tags=regression -v ./tests/regression/
Generate Test PCAP Programmatically
package main
import (
"github.com/dreadl0ck/netcap/helpers"
)
func main() {
// Generate HTTP request PCAP
err := helpers.GenerateHTTPRequest("test-http.pcap", "example.com")
if err != nil {
panic(err)
}
// Generate TCP handshake
err = helpers.GenerateTCPHandshake(
"test-tcp.pcap",
"192.168.1.1", "192.168.1.2",
12345, 80,
)
if err != nil {
panic(err)
}
}
๐ Troubleshooting
Build Failures
Problem: Tests fail to compile
# Check for syntax errors
go vet ./...
# Check imports
go mod tidy
# Rebuild everything
go clean -cache
go build ./...
Problem: Missing dependencies
# Update dependencies
go mod download
go mod tidy
Test Failures
Problem: Test fails intermittently (flaky test)
# Run test multiple times
go test -count=10 -run TestMyFlaky ./...
# Run with race detector
go test -race -run TestMyFlaky ./...
Problem: Test hangs
# Run with timeout
go test -timeout=30s ./...
# Add verbose output
go test -v -timeout=30s ./...
Missing Test Data
Problem: "Test PCAP not available" or "Golden file not found"
# Check if fixture exists
ls tests/fixtures/pcaps/protocols/
# Download test fixtures (if script exists)
make -f Makefile.test fixtures-download
# Or skip tests requiring fixtures
go test -short ./...
Golden File Mismatches
Problem: Regression test fails with "Files differ"
# View the difference
diff tests/fixtures/golden/csv/scenario/HTTP.csv /tmp/actual-output/HTTP.csv
# If change is intentional, update golden file
UPDATE_GOLDEN=1 go test -tags=regression -v ./tests/regression/
# Or manually
cp /tmp/actual-output/HTTP.csv tests/fixtures/golden/csv/scenario/
๐ Tracking Progress
Check Coverage
# Generate coverage report
go test -coverprofile=coverage.out ./...
# View in terminal
go tool cover -func=coverage.out
# View in browser
go tool cover -html=coverage.out
Run Coverage Check
# Ensure coverage doesn't drop below threshold
make -f Makefile.test test-coverage-check
Benchmark Performance
# Run benchmarks
go test -bench=. ./...
# With memory stats
go test -bench=. -benchmem ./...
# Save results for comparison
go test -bench=. -benchmem ./... | tee benchmark-results.txt
๐ฏ Test Development Workflow
For New Features
-
Write test first (TDD approach)
# Create test vim mypackage/myfeature_test.go # Run (should fail) go test ./mypackage/ -
Implement feature
vim mypackage/myfeature.go -
Verify test passes
go test -v ./mypackage/ -
Check coverage
go test -cover ./mypackage/
For Bug Fixes
- Write failing test reproducing the bug
- Fix the bug
- Verify test now passes
- Add to regression suite (if needed)
For Refactoring
-
Ensure existing tests pass
go test ./... -
Make refactoring changes
-
Run tests continuously
# Use entr or similar for auto-run find . -name '*.go' | entr -c go test ./... -
Verify coverage maintained
๐ Additional Resources
- Full Test Plan - Comprehensive 16-week plan
- Test Plan Summary - Executive overview
- Test Suite README - Detailed test documentation
- Go Testing Documentation
- Table-Driven Tests in Go
๐ค Contributing Tests
When submitting PRs:
- โ Add tests for new features
- โ Update tests for bug fixes
- โ Maintain or improve coverage
- โ Ensure all tests pass
- โ Update documentation if needed
- โ Follow test naming conventions
Test Naming Convention
// Format: Test<Component>_<Scenario>
func TestCollector_InitWithValidConfig(t *testing.T)
func TestDecoder_MalformedPacket(t *testing.T)
func TestHTTP_ParseRequest(t *testing.T)
// Format: Benchmark<Component>_<Operation>
func BenchmarkCollector_ProcessPacket(b *testing.B)
func BenchmarkDecoder_TCP(b *testing.B)
๐ Learning Path
Week 1: Basics
- Run existing tests
- Read test examples
- Write simple unit test
Week 2: Test Types
- Write table-driven test
- Create integration test
- Use test fixtures
Week 3: Advanced
- Add regression test
- Generate synthetic PCAP
- Create benchmark
Week 4: Contribution
- Fix failing test
- Improve coverage
- Submit PR with tests
Questions? Open an issue on GitHub or check the full TEST_PLAN.md
Last Updated: October 21, 2025