Step-by-Step Guide to Automating Laravel Integration Tests with GitHub Actions

Integrating automated tests into your Laravel project is essential for maintaining code quality and ensuring smooth deployments. GitHub Actions provides a powerful platform to automate testing workflows seamlessly. This guide walks you through setting up automated Laravel integration tests using GitHub Actions step-by-step.

Prerequisites

  • A Laravel project hosted on GitHub
  • Basic knowledge of GitHub Actions
  • Docker installed locally for testing

Step 1: Prepare Your Laravel Application

Ensure your Laravel application has a robust set of integration tests. Use Laravel’s built-in testing features with PHPUnit. Confirm that your tests can run locally without issues before automating them.

Step 2: Create a GitHub Workflow File

In your GitHub repository, create a directory named .github/workflows if it doesn’t exist. Inside, add a new file called laravel-tests.yml.

This file defines the automation workflow for your tests.

Sample Workflow Configuration

Paste the following configuration into laravel-tests.yml:

name: Laravel Integration Tests

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: rootpassword
          MYSQL_DATABASE: laravel_test
          MYSQL_USER: laravel
          MYSQL_PASSWORD: secret
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping --silent"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3

    steps:
      - uses: actions/checkout@v3

      - name: Set up PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'
          extensions: mbstring, xml, bcmath, pdo, pdo_mysql
          coverage: none

      - name: Install Composer Dependencies
        run: composer install --prefer-dist --no-progress --no-suggest

      - name: Copy Environment File
        run: cp .env.example .env

      - name: Generate Application Key
        run: php artisan key:generate

      - name: Configure Database
        run: |
          echo "DB_HOST=127.0.0.1" >> .env
          echo "DB_DATABASE=laravel_test" >> .env
          echo "DB_USERNAME=laravel" >> .env
          echo "DB_PASSWORD=secret" >> .env

      - name: Run Migrations
        run: php artisan migrate --force

      - name: Run Tests
        run: php artisan test --testsuite=Feature

Step 3: Configure Database Settings

Ensure your .env.example file is configured for testing. The workflow overrides database credentials to connect to the MySQL service spun up by GitHub Actions.

Step 4: Push Your Workflow and Test

Commit and push your laravel-tests.yml file to GitHub. The workflow will trigger on pushes or pull requests to the main branch.

Navigate to the Actions tab in GitHub to monitor your test runs. If configured correctly, your Laravel integration tests will run automatically, providing instant feedback on your code changes.

Additional Tips

  • Use secrets for sensitive data like API keys or passwords.
  • Adjust the PHP version and extensions based on your project requirements.
  • Include cache steps to speed up dependency installation.
  • Run additional tests, such as unit tests, in parallel jobs for comprehensive coverage.

Automating Laravel integration tests with GitHub Actions streamlines your development workflow, catches issues early, and maintains high-quality code. Implementing this setup ensures your application remains robust through continuous testing.