Skip to content
intermediate Phase 74 · Testing Advanced

CI Testing

45m
1 problems
Topic Progress 0%

GitHub Actions Setup

Magento CI Workflow

# .github/workflows/magento-tests.yml
name: Magento Tests

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  unit-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'
          extensions: mbstring, intl, gd, xsl
          coverage: xdebug
      
      - name: Install dependencies
        run: composer install --prefer-dist --no-progress
      
      - name: Run unit tests
        run: vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist
      
      - name: Upload coverage
        uses: codecov/codecov-action@v3
        with:
          files: dev/tests/unit/coverage/clover.xml

  integration-tests:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: magento_test
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5
      
      redis:
        image: redis:6
        ports:
          - 6379:6379
    
    steps:
      - uses: actions/checkout@v3
      
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'
          extensions: mbstring, intl, gd, xsl, redis
      
      - name: Install dependencies
        run: composer install --prefer-dist --no-progress
      
      - name: Setup Magento
        run: |
          bin/magento setup:install \
            --db-host=127.0.0.1 \
            --db-name=magento_test \
            --db-user=root \
            --db-password=root \
            --admin-user=admin \
            --admin-password=admin123
      
      - name: Run integration tests
        run: vendor/bin/phpunit -c dev/tests/integration/phpunit.xml.dist

Key Points

  • Use services for databases (MySQL, Redis)
  • Cache composer dependencies
  • Run tests in parallel when possible
  • Upload coverage reports

GitLab CI Configuration

GitLab Pipeline

# .gitlab-ci.yml
stages:
  - setup
  - test
  - deploy

variables:
  MYSQL_DATABASE: magento_test
  MYSQL_ROOT_PASSWORD: root

services:
  - mysql:8.0
  - redis:6

composer:
  stage: setup
  image: php:8.1-cli
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - vendor/
  script:
    - composer install --prefer-dist --no-progress
  artifacts:
    paths:
      - vendor/
    expire_in: 1 day

unit-tests:
  stage: test
  image: php:8.1-cli
  dependencies:
    - composer
  script:
    - vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist
  coverage: '/Lines:\s*(\d+\.?\d*)%/'
  artifacts:
    reports:
      junit: dev/tests/unit/test-results.xml
      coverage_report:
        coverage_format: cobertura
        path: dev/tests/unit/coverage.xml

integration-tests:
  stage: test
  image: php:8.1-cli
  dependencies:
    - composer
  script:
    - bin/magento setup:install --db-name=$MYSQL_DATABASE --db-user=root --db-password=$MYSQL_ROOT_PASSWORD
    - vendor/bin/phpunit -c dev/tests/integration/phpunit.xml.dist
  allow_failure: false

mftf:
  stage: test
  image: php:8.1-cli
  dependencies:
    - composer
  script:
    - vendor/bin/mftf run:suite adminSuite
  artifacts:
    when: always
    paths:
      - dev/tests/functional/Mftf/Allure/

Key Points

  • Use stages for test organization
  • Cache dependencies between jobs
  • Generate test reports for GitLab UI
  • Allow failure control for critical tests

Test Automation

Automated Test Runner

// bin/run-tests.php
<?php
$tests = [
    'unit' => 'dev/tests/unit/phpunit.xml.dist',
    'integration' => 'dev/tests/integration/phpunit.xml.dist',
];

$results = [];

foreach ($tests as $type => $config) {
    $output = [];
    $exitCode = 0;
    
    exec("vendor/bin/phpunit -c {$config} --testdox", $output, $exitCode);
    
    $results[$type] = [
        'status' => $exitCode === 0 ? 'passed' : 'failed',
        'output' => implode("\n", $output),
    ];
}

// Generate report
$report = generateReport($results);
file_put_contents('test-report.json', json_encode($report));

echo $report['summary'];

Pre-commit Hooks

#!/bin/bash
# .git/hooks/pre-commit

echo "Running unit tests..."
vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist --quiet

if [ $? -ne 0 ]; then
    echo "Unit tests failed. Commit aborted."
    exit 1
fi

echo "Running linting..."
vendor/bin/phpcs --standard=PSR2 app/code/Vendor/Module/

if [ $? -ne 0 ]; then
    echo "Linting failed. Commit aborted."
    exit 1
fi

echo "All checks passed. Proceeding with commit."

Key Points

  • Automate test execution on commits/PRs
  • Use pre-commit hooks for fast feedback
  • Generate and publish test reports
  • Fail builds on test failures

Parallel Testing

PHPUnit Parallel Execution

<!-- phpunit.xml.dist -->
<phpunit>
    <testsuites>
        <testsuite name="Suite 1">
            <directory>Test/Unit/Model</directory>
        </testsuite>
        <testsuite name="Suite 2">
            <directory>Test/Unit/Helper</directory>
        </testsuite>
        <testsuite name="Suite 3">
            <directory>Test/Unit/Controller</directory>
        </testsuite>
    </testsuites>
</phpunit>

GitHub Actions Matrix

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        test-suite: [unit, integration, api]
        php-version: ['8.1', '8.2']
    steps:
      - uses: actions/checkout@v3
      - name: Setup PHP ${{ matrix.php-version }}
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-version }}
      - name: Run ${{ matrix.test-suite }} tests
        run: vendor/bin/phpunit -c dev/tests/${{ matrix.test-suite }}/phpunit.xml.dist

Docker Parallel Testing

# docker-compose.test.yml
version: '3.8'
services:
  test-runner-1:
    build: .
    command: vendor/bin/phpunit --testsuite "Suite 1"
  
  test-runner-2:
    build: .
    command: vendor/bin/phpunit --testsuite "Suite 2"
  
  test-runner-3:
    build: .
    command: vendor/bin/phpunit --testsuite "Suite 3"

Key Points

  • Split tests into parallel groups
  • Use matrix builds for multiple versions
  • Docker containers for isolated environments
  • Aggregate results from parallel runs

Practice Problems

0 / 1 solved
CI Pipeline Setup

Create a GitHub Actions workflow that runs unit and integration tests on pull requests.

Solution
name: Tests

on: [pull_request]

jobs:
  unit-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'
          coverage: xdebug
      - run: composer install --prefer-dist
      - run: vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist
  
  integration-tests:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: test
        ports:
          - 3306:3306
    steps:
      - uses: actions/checkout@v3
      - uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'
      - run: composer install --prefer-dist
      - run: bin/magento setup:install --db-name=test --db-user=root --db-password=root
      - run: vendor/bin/phpunit -c dev/tests/integration/phpunit.xml.dist

Quiz

1. Why use services in GitHub Actions?

Question 1 options

2. What does parallel testing improve?

Question 2 options

3. What is a pre-commit hook?

Question 3 options

4. Why cache composer dependencies?

Question 4 options

Flashcards

Question

CI services purpose?

Answer

Provide databases (MySQL, Redis) for tests

Question

Parallel testing benefit?

Answer

Reduces total test execution time

Question

Pre-commit hook?

Answer

Runs checks before code is committed

Question

Cache composer deps?

Answer

Speeds up CI builds, saves network

Revision Notes

Key Takeaways

  • 1. Use GitHub Actions or GitLab CI for test automation
  • 2. Services provide databases for integration tests
  • 3. Parallel testing reduces execution time
  • 4. Pre-commit hooks catch issues early

Interview Tips

  • Explain CI/CD testing pipeline setup
  • Discuss parallel testing strategies
  • Know how to configure test services

Cheat Sheet

CI Testing

  • GitHub Actions: .github/workflows/
  • GitLab CI: .gitlab-ci.yml
  • Services: MySQL, Redis
  • Parallel: matrix builds
  • Cache: composer dependencies