TEST RELIABILITY QUICKSTART - nself-org/cli GitHub Wiki

Test Reliability Quick Start

Get started with bulletproof testing in 5 minutes

What You Get

  • โœ… 100% reliable tests (zero flakiness)
  • โœ… Fast execution (<5 seconds per test)
  • โœ… No external dependencies (mocks for Docker, network, etc.)
  • โœ… Automatic cleanup (no leftover resources)
  • โœ… Cross-platform (macOS, Linux, WSL)

Quick Example

#!/usr/bin/env bash
# my-test.sh - Example reliable test

# Source the framework
source src/tests/lib/reliable-test-framework.sh
source src/tests/mocks/docker-mock.sh

# Your test function
test_my_feature() {
  # Create isolated test directory (auto-cleanup)
  local test_dir=$(create_isolated_test_dir)
  cd "$test_dir"

  # Run commands with mocks (instant, no Docker needed)
  docker run --name test-app nginx

  # Verify
  if docker ps | grep -q "test-app"; then
    echo "โœ“ Test passed"
    return 0
  else
    echo "โœ— Test failed"
    return 1
  fi
}

# Run with timeout protection
run_test_with_timeout test_my_feature 30

Run it:

bash my-test.sh
# โœ“ Test passed (completes in <1 second)

Install Pre-Commit Hook

bash scripts/install-pre-commit-hook.sh

Now every commit runs fast checks automatically.

See It In Action

# Run the complete example showing all features
bash src/tests/examples/reliable-test-example.sh

Output:

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘         Reliable Test Framework Examples                  โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

โ†’ Test 1: Basic test with timeout and cleanup
  โœ“ Test file created successfully
  โœ“ Test completed within timeout

โ†’ Test 2: Guaranteed cleanup (even on failure)
  Setup: Created resource at /tmp/tmp.XXXXXXXXXX
  โœ“ Resource created in setup directory
  Cleanup: Removed resource
  โœ“ Test passed and cleanup executed

โ†’ Test 3: Using Docker mock (fast, no Docker required)
  โœ“ Container created (mocked)
  โœ“ Container lifecycle tested without real Docker

โ†’ Test 4: Using network mock (no real HTTP requests)
  โœ“ API request mocked successfully
  โœ“ Error response handled correctly

โ†’ Test 5: Using time mock (instant timeouts)
  โœ“ Time advanced 60 seconds instantly
  โœ“ Time multiplier works (10x speed)

... (10 tests total)

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Test Summary:
  Total:  24
  Passed: 24
  Failed: 0
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โœ“ All examples passed!

Common Patterns

Pattern 1: Test with Auto-Cleanup

source src/tests/lib/reliable-test-framework.sh

test_creates_files() {
  # Auto-cleanup test directory
  local test_dir=$(create_isolated_test_dir)
  cd "$test_dir"

  # Create files
  echo "test" > file.txt

  # Verify
  [[ -f "file.txt" ]] && echo "โœ“ File created"

  # Cleanup happens automatically
}

run_test_with_timeout test_creates_files 10

Pattern 2: Test with Docker Mock

source src/tests/mocks/docker-mock.sh

test_docker_operations() {
  # No Docker required!
  docker run --name test nginx
  docker ps
  docker stop test
  docker rm test

  echo "โœ“ Docker operations tested (mocked)"
}

run_test_with_timeout test_docker_operations 10

Pattern 3: Test with Network Mock

source src/tests/mocks/network-mock.sh

test_api_call() {
  # Setup mock response
  register_mock_response \
    "https://api.example.com/status" \
    200 \
    '{"status": "ok"}'

  # Make request (instant, no network)
  response=$(curl -s https://api.example.com/status)

  echo "$response" | grep -q "ok" && echo "โœ“ API call tested"
}

run_test_with_timeout test_api_call 10

Pattern 4: Test with Retry Logic

source src/tests/lib/reliable-test-framework.sh

test_flaky_operation() {
  ATTEMPT=0

  flaky_func() {
    ATTEMPT=$((ATTEMPT + 1))
    [[ $ATTEMPT -ge 3 ]] && return 0 || return 1
  }

  # Retry up to 5 times
  retry_on_failure flaky_func 5 && echo "โœ“ Flaky operation handled"
}

run_test_with_timeout test_flaky_operation 10

Tools

Find Flaky Tests

bash scripts/find-flaky-tests.sh --iterations 10
# Runs each test 10 times and reports flakiness

Analyze Performance

bash scripts/test-performance-analysis.sh
# Shows slow tests and suggests optimizations

Next Steps

  1. Read the full guide: docs/development/TEST-RELIABILITY-GUIDE.md
  2. See implementation details: docs/development/TEST-RELIABILITY-IMPLEMENTATION.md
  3. Run examples: bash src/tests/examples/reliable-test-example.sh
  4. Install pre-commit hook: bash scripts/install-pre-commit-hook.sh
  5. Migrate your tests to use the framework

Key Benefits

Before After
Tests flaky (50-90% pass rate) 100% pass rate
Slow (30+ seconds per test) Fast (<5 seconds)
Docker required Mocked (instant)
Network required Mocked (offline works)
Cleanup manual Automatic
Hard to debug Clear error messages

Quick Reference

# Framework
source src/tests/lib/reliable-test-framework.sh

# Timeout
run_test_with_timeout my_test 30

# Cleanup
with_cleanup test_func cleanup_func

# Isolation
test_dir=$(create_isolated_test_dir)
port=$(get_random_port)

# Retry
retry_on_failure flaky_test 3

# Wait
wait_for_condition is_ready 30

# Mocks
source src/tests/mocks/docker-mock.sh      # Docker
source src/tests/mocks/network-mock.sh     # HTTP
source src/tests/mocks/time-mock.sh        # Time
source src/tests/mocks/filesystem-mock.sh  # Files

Summary

You now have a production-ready test infrastructure that ensures:

  • Zero flakiness
  • Fast execution
  • Complete isolation
  • Cross-platform compatibility
  • Easy debugging

Start writing bulletproof tests today!

โš ๏ธ **GitHub.com Fallback** โš ๏ธ