Skip to content
intermediate Phase 74 · Testing Advanced

Test Pyramid

45m
1 problems
Topic Progress 0%

Unit/Integration/E2E Ratios

Test Pyramid Structure

        /
       / E2E  (10%)
      /------\
     / Integr (20%)
    /----------\
   /   Unit    (70%)
  /--------------\

Recommended Ratios

test_strategy:
  unit_tests:
    percentage: 70
    speed: fast
    confidence: low
    cost: low
    
  integration_tests:
    percentage: 20
    speed: medium
    confidence: medium
    cost: medium
    
  e2e_tests:
    percentage: 10
    speed: slow
    confidence: high
    cost: high

Magento Test Distribution

// Example test distribution for a module
class TestDistribution
{
    public function getDistribution(): array
    {
        return [
            'unit' => [
                'Model' => 15,
                'Helper' => 8,
                'Service' => 12,
            ],
            'integration' => [
                'Model' => 5,
                'Controller' => 3,
                'API' => 4,
            ],
            'e2e' => [
                'AdminWorkflow' => 2,
                'CustomerJourney' => 2,
            ],
        ];
    }
}

Key Points

  • More unit tests for fast feedback
  • Fewer E2E tests for critical paths
  • Integration tests verify component interactions
  • Adjust ratios based on project needs

Testing Strategy

Magento Testing Strategy

class TestingStrategy
{
    public function getStrategy(): array
    {
        return [
            'unit' => [
                'focus' => 'Business logic',
                'scope' => 'Single class/method',
                'dependencies' => 'Mocked',
                'frequency' => 'Every commit',
            ],
            'integration' => [
                'focus' => 'Component interactions',
                'scope' => 'Multiple classes',
                'dependencies' => 'Real database/services',
                'frequency' => 'Every PR',
            ],
            'e2e' => [
                'focus' => 'User workflows',
                'scope' => 'Complete features',
                'dependencies' => 'Full application',
                'frequency' => 'Nightly/release',
            ],
        ];
    }
}

What to Test at Each Level

## Unit Tests
- Business logic calculations
- Data transformation
- Validation rules
- Error handling
- Edge cases

## Integration Tests
- Database operations
- API endpoints
- Service interactions
- Configuration loading
- Event handling

## E2E Tests
- User registration/login
- Product browsing
- Checkout process
- Admin workflows
- Critical business flows

Key Points

  • Unit tests for isolated logic
  • Integration tests for component wiring
  • E2E tests for critical user journeys
  • Avoid testing the same thing at multiple levels

Test Coverage

Measuring Coverage

# Generate unit test coverage
vendor/bin/phpunit --coverage-html dev/tests/unit/coverage \
    -c dev/tests/unit/phpunit.xml.dist

# Generate integration test coverage
vendor/bin/phpunit --coverage-html dev/tests/integration/coverage \
    -c dev/tests/integration/phpunit.xml.dist

# Coverage summary
vendor/bin/phpunit --coverage-text \
    -c dev/tests/unit/phpunit.xml.dist

Coverage Targets

coverage_targets:
  minimum: 60%
  recommended: 80%
  ideal: 90%

by_type:
  models: 80%
  helpers: 70%
  controllers: 60%
  blocks: 50%
  api: 90%

Coverage Configuration

<!-- phpunit.xml.dist -->
<coverage>
    <include>
        <directory>../../../app/code/Vendor/Module</directory>
    </include>
    <exclude>
        <directory>../../../app/code/Vendor/Module/Test</directory>
        <file>../../../app/code/Vendor/Module/Setup/*</file>
    </exclude>
    <report>
        <html outputDirectory="coverage"/>
        <text outputFile="coverage.txt" showUncoveredFiles="true"/>
    </report>
</coverage>

Key Points

  • Coverage is a metric, not a goal
  • Focus on meaningful coverage (critical code)
  • High coverage doesn't guarantee quality
  • Review coverage reports regularly

Practice Problems

0 / 1 solved
Test Strategy Design

Design a testing strategy for a new Magento module including unit, integration, and E2E tests.

Solution
// Testing Strategy for Vendor_ProductWidget

// Unit Tests (70%)
// - ProductWidgetHelper: calculations, formatting
// - ProductWidgetModel: data handling, validation
// - ProductWidgetBlock: output generation

// Integration Tests (20%)
// - ProductWidgetController: request handling
// - ProductWidgetRepository: database operations
// - ProductWidgetAPI: REST/GraphQL endpoints

// E2E Tests (10%)
// - Admin: configure widget settings
// - Frontend: display widget on page
// - Customer: interact with widget

// Execution Plan
// - Unit: every commit (fast)
// - Integration: every PR (medium)
// - E2E: nightly (slow)

Quiz

1. What is the ideal ratio of unit to E2E tests?

Question 1 options

2. What do integration tests verify?

Question 2 options

3. What is a good minimum coverage target?

Question 3 options

4. When should E2E tests run?

Question 4 options

Flashcards

Question

Test pyramid ratio?

Answer

70% unit, 20% integration, 10% E2E

Question

Integration test focus?

Answer

Component interactions and data flow

Question

Minimum coverage?

Answer

60% minimum, 80%+ recommended

Question

E2E test timing?

Answer

Nightly or before releases (slow)

Revision Notes

Key Takeaways

  • 1. Follow the test pyramid: 70/20/10 ratio
  • 2. Unit tests for logic, integration for wiring, E2E for workflows
  • 3. Coverage is a metric, not the only goal
  • 4. Balance test speed with confidence

Interview Tips

  • Explain the test pyramid concept
  • Discuss when to use each test type
  • Know how to measure and improve coverage

Cheat Sheet

Test Pyramid

  • Unit (70%): fast, isolated
  • Integration (20%): component interactions
  • E2E (10%): user workflows, slow
  • Coverage: 60% min, 80% ideal