Get started with 33% off your first certification using code: 33OFFNEW

Using GitHub Actions to Run Laravel Pest Tests

2 min read
Published on 19th September 2023
Using GitHub Actions to Run Laravel Pest Tests

Continuous Integration (CI) has become a crucial part of modern development workflows. GitHub Actions offers a convenient and powerful platform for setting up CI pipelines right within your GitHub repositories. When combined with Laravel and its expressive Pest testing tool, you can ensure that your application maintains high code quality standards with every commit.

1. Setting Up GitHub Actions

To get started, you'll need a .github/workflows directory in your Laravel project. Here, you can define the workflows for your project.

Create a new file inside this directory, named pest-tests.yml.

name: Run Pest Tests

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

jobs:
  run-tests:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

2. Setting up the Testing Environment

Before running the tests, you need to set up PHP and any other services, such as MySQL:

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: 8.0

    - name: Install dependencies
      run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist

    - name: Set up Laravel environment
      run: cp .env.testing .env

3. Running Pest Tests

With everything set up, you can now instruct GitHub Actions to run your Pest tests:

    - name: Run Pest tests
      run: ./vendor/bin/pest

4. Caching Dependencies

To speed up your test runs, consider caching your composer dependencies:

    - name: Get Composer cache directory
      id: composer-cache
      run: echo "::set-output name=dir::$(composer config cache-files-dir)"

    - name: Cache dependencies
      uses: actions/cache@v2
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: |
          ${{ runner.os }}-composer-

By integrating Pest tests into your GitHub Actions pipeline, you enhance the reliability and quality of your Laravel application. Not only does this setup catch potential errors early in the development process, but it also ensures that every push or pull request meets the required code standards.

Automating tests using GitHub Actions with Pest provides a seamless and efficient CI/CD experience, ensuring your Laravel applications are robust, error-free, and ready for production.