Integration Testing

What is integration testing?

Integration testing verifies that different parts of your application work together correctly when combined.

Benefits of integration testing?

  • Test interaction between components
  • Verify data flow between systems
  • Catch issues before production
  • Ensure game mechanics work together
  • Build confidence in code changes
// Integration test: Player and Enemy interaction
function testPlayerEnemyInteraction() {
    // Create player
    const player = { x: 100, y: 300, health: 100 };
    
    // Create enemy
    const enemy = { x: 150, y: 300, health: 50 };
    
    // Test: Check if in range
    const distance = Math.abs(player.x - enemy.x);
    const inRange = distance < 60;
    console.log("In range:", inRange); // Should be true
    
    // Test: Simulate attack
    enemy.health -= 10;
    console.log("Enemy health:", enemy.health); // Should be 40
    
    // Test: Enemy dies
    if (enemy.health <= 0) {
        console.log("Test passed: Enemy defeated");
    }
}

What does this code do?

  • Tests player and enemy can detect each other
  • Tests distance calculation between objects
  • Tests attack mechanics
  • Verifies enemy health decreases
  • Tests enemy defeat condition
  • Shows how to write integration tests
// Comprehensive integration test suite
function runGameTests() {
    let passedTests = 0;
    let failedTests = 0;
    
    // Test 1: Game initialization
    try {
        const gameEnv = { width: 800, height: 600 };
        console.assert(gameEnv.width > 0, "Canvas width invalid");
        passedTests++;
    } catch (e) {
        console.error("Initialization test failed:", e);
        failedTests++;
    }
    
    // Test 2: Player and level integration
    try {
        const player = { level: 1, score: 0 };
        const level = { number: 1, enemyCount: 3 };
        console.assert(player.level === level.number, "Level mismatch");
        passedTests++;
    } catch (e) {
        console.error("Level integration test failed:", e);
        failedTests++;
    }
    
    // Test 3: Save/load data
    try {
        const data = { player: { name: 'Astro' } };
        const json = JSON.stringify(data);
        const loaded = JSON.parse(json);
        console.assert(loaded.player.name === 'Astro', "Data corruption");
        passedTests++;
    } catch (e) {
        console.error("Save/load test failed:", e);
        failedTests++;
    }
    
    console.log(`Tests: ${passedTests} passed, ${failedTests} failed`);
}

What does this code do?

  • Runs multiple integration tests
  • Tests game initialization
  • Tests level-player integration
  • Tests data serialization
  • Tracks passed and failed tests
  • Shows comprehensive testing approach
  • Demonstrates assertions and error handling

layout: post title: Integration Testing description: Learn about integration testing permalink: /integration_testing author: Vihaan Budhraja —

Try It Yourself

%%js // CODE_RUNNER: integration-testing function test(name, fn) { try { fn(); console.log(“✓ “ + name); } catch (e) { console.log(“✗ “ + name); } }

test(“Player creation”, () => { const player = { health: 100 }; if (player.health !== 100) throw new Error(“Failed”); });

test(“Damage calculation”, () => { if (100 - 25 !== 75) throw new Error(“Failed”); });