Testing DNS-MNS
DNS-MNS includes a comprehensive test suite that covers both the Go binary and the Bash script. This guide explains how to run the tests and what they cover.
Test Structure
tests/
├── bash/ # Bash tests using Bats
├── integration/ # Go integration tests
└── unit/ # Go unit tests (within each package)
Running Tests
Quick Test (Recommended for Development)
Run all tests without network requirements:
This runs the short test suite which completes in seconds.
Full Test Suite
Run all tests including network tests:
Network tests require internet connectivity and may take longer to complete.
Go Tests
Unit Tests
Each internal package has comprehensive unit tests:
# Test all packages
go test -v ./...
# Test specific package
go test -v ./internal/dnslist/
# Test with coverage
go test -cover ./...
Test Coverage
Generate a coverage report:
This creates coverage.html which you can open in your browser to see detailed coverage information.
Integration Tests
Integration tests verify the complete workflow:
These tests:
- Load the embedded DNS list
- Test ping functionality
- Test DNS resolution
- Verify platform detection
- Build the binary
- Test cross-platform compilation
Race Detection
Run tests with the race detector:
Bash Tests
The Bash script is tested using Bats (Bash Automated Testing System).
Install Bats
Run Bash Tests
make test-bats
# or
bats tests/bash/
Test Categories
| Test File | Description |
|---|
test_dnslist.bats | Validates DNS list format and content |
test_script.bats | Tests script structure and syntax |
test_ping.bats | Tests ping functionality |
test_utils.bats | Tests helper functions |
test_integration.bats | Integration tests |
What the Tests Cover
DNS List Tests
- ✅ File exists and is readable
- ✅ Contains all expected categories
- ✅ All entries have valid format (NAME|IP|IP|LOCATION)
- ✅ All IPs are valid IPv4 addresses
- ✅ No malformed entries
- ✅ Categories are properly defined
Go Package Tests
internal/dnslist
- DNS list parsing from string and embedded file
- Category filtering
- Entry validation
internal/ping
- Platform-specific ping output parsing
- Average time calculation
- Timeout handling
internal/dnstest
- DNS resolution timing
- Timeout handling
- Error cases
internal/doh
- DoH/DoT provider testing
- Result structure validation
- Provider list validation
internal/dnscrypt
- DNSCrypt provider testing
- Proxy functionality
- Result structure validation
internal/ui
- Color formatting
- Progress bar rendering
- Message formatting
internal/setter
- Platform detection
- Admin privilege checking
- OS-specific code paths
Integration Tests
- ✅ Binary can be built for all platforms
- ✅ Embedded DNS list loads correctly
- ✅ Complete workflow from loading to testing
- ✅ Cross-platform compatibility
Local Testing
All testing is done locally using the Makefile and Go test commands. There is no CI/CD pipeline — run tests on your machine before committing:
make test # Quick short tests (no network)
make test-full # Full suite including network tests
make test-race # Race condition detection
make coverage # Generate coverage report
Writing New Tests
Go Tests
Follow the existing patterns in *_test.go files:
func TestNewFeature(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"valid case", "input", "expected"},
{"edge case", "", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := NewFeature(tt.input)
if result != tt.expected {
t.Errorf("NewFeature(%q) = %q, want %q",
tt.input, result, tt.expected)
}
})
}
}
Bash Tests
@test "description of what this test verifies" {
# Run command
run some_command
# Check exit status
[ "$status" -eq 0 ]
# Check output
[[ "$output" == *"expected content"* ]]
}
Test Best Practices
- Use table-driven tests for testing multiple inputs
- Skip network tests when running in short mode (
if testing.Short())
- Skip admin tests when not running as root
- Use temporary directories for file operations
- Clean up after tests using
t.TempDir() or teardown()
Troubleshooting
Tests fail with “permission denied”
Some tests require root/admin privileges. These are automatically skipped when not available.
Network tests timeout
Network tests may fail in restricted environments. Use make test (short mode) to skip them.
Bats tests not found
Make sure Bats is installed and in your PATH:
If not installed, see the Bats installation guide.
Makefile Commands
| Command | Description |
|---|
make test | Run short tests (fast) |
make test-full | Run all tests |
make test-bats | Run Bash tests |
make test-integration | Run integration tests |
make test-race | Run tests with race detector |
make coverage | Generate coverage report |
make lint | Run all linters |