A comprehensive testing skill for MQTT X applications, providing templates, scripts, and best practices for writing high-quality tests.
mqttx-testing/ ├── SKILL.md # Main skill documentation ├── README.md # This file ├── assets/ # Test templates │ ├── unit-test.go.tmpl # Unit test template │ ├── integration-test.go.tmpl # Integration test template │ └── benchmark-test.go.tmpl # Benchmark test template ├── scripts/ # Automation scripts │ └── run-tests.sh # Test execution script (executable) └── references/ # Reference documentation └── testing-best-practices.md # Comprehensive testing guide
cat docs/skills/mqttx-testing/SKILL.md
The templates in assets/ provide starting points for different test types:
# Make script executable (if not already)
chmod +x docs/skills/mqttx-testing/scripts/run-tests.sh
# Run all tests
./docs/skills/mqttx-testing/scripts/run-tests.sh
# Run with options
./docs/skills/mqttx-testing/scripts/run-tests.sh --help
cat docs/skills/mqttx-testing/references/testing-best-practices.md
Provides:
Variables to replace:
{{.PackageName}}: Package name (e.g., mqttx){{.FeatureName}}: Feature being tested (e.g., Session){{.TestCase1}}, {{.TestCase2}}: Test case namesProvides:
Variables to replace:
{{.PackageName}}: Package name{{.FeatureName}}: Feature being tested{{.BrokerAddress}}: MQTT broker address{{.Username}}: MQTT username{{.Password}}: MQTT passwordProvides:
Variables to replace:
{{.PackageName}}: Package name{{.FeatureName}}: Feature being benchmarked# Show help
./docs/skills/mqttx-testing/scripts/run-tests.sh --help
# Run all unit tests with coverage
./docs/skills/mqttx-testing/scripts/run-tests.sh
# Run short tests only
./docs/skills/mqttx-testing/scripts/run-tests.sh -s
# Run with race detector
./docs/skills/mqttx-testing/scripts/run-tests.sh -r
# Run integration tests
./docs/skills/mqttx-testing/scripts/run-tests.sh -i
# Run benchmarks
./docs/skills/mqttx-testing/scripts/run-tests.sh -b
# Verbose output
./docs/skills/mqttx-testing/scripts/run-tests.sh -v
# Multiple options
./docs/skills/mqttx-testing/scripts/run-tests.sh -s -r -v
The references/testing-best-practices.md document covers:
-race flag, writing race-free codecp docs/skills/mqttx-testing/assets/unit-test.go.tmpl session_feature_test.go
// Change:
package {{.PackageName}}
// To:
package mqttx
// Change:
func Test{{.FeatureName}}(t *testing.T)
// To:
func TestSessionPublish(t *testing.T)
Add your specific test cases to the table-driven test structure.
go test -v -run TestSessionPublish
# 1. Write tests first (TDD)
# 2. Run short tests during development
go test -short ./...
# 3. Run full tests before commit
go test ./...
# 4. Check coverage
go test -cover ./...
# 5. Run race detector
go test -race ./...
# 6. Run benchmarks for performance-critical changes
go test -bench=. -benchmem
# In CI pipeline
./docs/skills/mqttx-testing/scripts/run-tests.sh -r -v
# Generate coverage report
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
# Upload coverage to codecov or similar
| Coverage | Status | Action |
|---|---|---|
| 80-100% | Excellent | Maintain |
| 60-80% | Good | Improve critical paths |
| 40-60% | Fair | Add more tests |
| 0-40% | Poor | Urgent: Add comprehensive tests |
tests := []struct {
name string
input interface{}
want interface{}
wantErr error
}{
// Test cases
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test logic
})
}
func TestFeature(t *testing.T) {
// Setup
manager := NewSessionManager()
defer manager.Close()
// Test logic
}
type mockClient struct {
connected bool
calls int
}
func (m *mockClient) Method() {
m.calls++
}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// Concurrent operation
}()
}
wg.Wait()
received := make(chan *Message, 1)
go func() {
// Async operation
}()
select {
case msg := <-received:
// Verify message
case <-time.After(5 * time.Second):
t.Fatal("Timeout")
}
-race to detect race conditions-count=100 to reproduce flaky testsgo tool cover -func=coverage.out | grep -v "100.0%"/workspace/docs//workspace/*_test.goWhen adding new test patterns or templates:
assets/ directorySKILL.mdreferences/testing-best-practices.mdSame as MQTT X project.