CodingTricks LogoCodingTricks
HomePostsTipsCopy/PasteLinksContact UsAbout Us
2024 - 2025 CodingTricks.co | All rights reserved
Privacy PolicyTerms of Service
Automate Laravel Testing with GitHub Actions

Automate Laravel Testing with GitHub Actions

Posted by

kamlesh paul

on

Dec 31, 2024

| 3 min read

Last updated on : Jan 2, 2025

Github ActionTesting
208 views

#Table of contents

  • Introduction
  • Why Automate Laravel Testing?
  • Prerequisites
  • Setting Up GitHub Actions for Laravel Testing
    • Step 1: Create a test.yml File
    • Step 2: Define the Workflow Trigger
    • Step 3: Set Up the Testing Environment
    • Step 4: Install Dependencies
    • Step 5: Run PHPUnit Tests
  • Conclusion

#Introduction

Automating testing in Laravel is a critical step in ensuring your application runs smoothly. Integrating GitHub Actions with Laravel allows you to automatically run tests whenever you push code or create pull requests, reducing the chance of errors slipping through. In this guide, we’ll walk you through setting up GitHub Actions to automate Laravel testing, focusing on unit and feature tests.

#Why Automate Laravel Testing?

Automating Laravel testing offers several benefits:

  • Continuous Integration: Ensure that your codebase is always in a deployable state.
  • Early Error Detection: Catch issues before they make it to production.
  • Consistency: Run the same tests across multiple environments automatically.

#Prerequisites

Before we begin, make sure you have:

  • A Laravel application set up in a GitHub repository.
  • Basic knowledge of PHPUnit or pestphp for testing in Laravel.
  • A GitHub Actions workflow configured in your project.

#Setting Up GitHub Actions for Laravel Testing

#Step 1: Create a test.yml File

  • Start by creating a .github/workflows/test.yml file in your repository. This file will define the steps GitHub Actions will take to run your tests.
.github/workflows/test.yml
name: Tests Cases
 
on:
    push:
      branches: [main, dev]
    pull_request:
      branches: [main, dev]
 
jobs:
  tests:
    name: Run tests
    runs-on: ubuntu-latest
    services:
      mysql-service:
        image: mysql:8.0
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_ROOT_PASSWORD: ""
          MYSQL_DATABASE: test
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3
 
    steps:
      - name: Checkout
        uses: actions/checkout@v3
 
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.2
          tools: composer:v2
          coverage: xdebug
 
      - name: Install Dependencies
        run: |
          cp .env.example .env
          composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
          php artisan key:generate
 
      - name: Execute tests (Unit and Feature tests) via PHPUnit
        env:
            DB_CONNECTION: test_mysql
            DB_DATABASE_TEST: test
            DB_PORT: 3306
            DB_USER: root
            DB_PASSWORD: ""
            APPLE_PAY_PASSWORD: "password"
        run: |
            php artisan test

#Step 2: Define the Workflow Trigger

  • The on: section of the YAML file defines when the workflow should be triggered. In this case, we’ve configured it to run on pushes and pull requests to the main and dev branches.

#Step 3: Set Up the Testing Environment

  • The jobs: section specifies the environment in which your tests will run. The runs-on: ubuntu-latest line tells GitHub to use the latest Ubuntu environment. Next, the services: section defines a MySQL service that your Laravel application will connect to during testing. The health checks ensure that the database service is ready before running your tests.

#Step 4: Install Dependencies

  • The steps: section details the actions GitHub will take to set up and run your tests. It starts with checking out your repository’s code and setting up PHP with the specified version. Then, we install the necessary dependencies using Composer, copy the example environment file to .env, and generate an application key.

#Step 5: Run PHPUnit Tests

  • Finally, the workflow runs your tests using the php artisan test command. The environment variables needed for the database connection are set up to ensure that your tests run correctly.

#Conclusion

By following this guide, you’ve set up a GitHub Actions workflow to automate testing in your Laravel application. This setup ensures that your code is continuously tested, allowing you to catch issues early and maintain a stable codebase.

Related Posts

  • Automating Next.js Deployment to VPS with GitHub Actions CI/CDAutomating Next.js Deployment to VPS with GitHub Actions CI/CD

Tags

Api(1)Authentication(5)Backup (1)Copy Paste(12)Email(2)Express(1)Firebase(1)Github Action(2)News(8)Push Notification(1)Queue(2)Server(11)Server Action(3)Testing(1)Tips(17)Websocket(1)

Popular Posts

  • TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!  TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!
  • How to use WebSocket in NextJS App router with Socket.IOHow to use WebSocket in NextJS App router with Socket.IO
  • How to Set Up Queue Jobs in NextJS Using BullMQHow to Set Up Queue Jobs in NextJS Using BullMQ
  • Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)
  • How to Set Up NextJS cron jobs without VercelHow to Set Up NextJS cron jobs without Vercel
  • Mastering Laravel Streamed Responses: Boost Performance with Fast Data DeliveryMastering Laravel Streamed Responses: Boost Performance with Fast Data Delivery
  • Tinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHPTinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHP
  • Nextjs 14 roles and permissions (RBAC) : Step-by-Step GuideNextjs 14 roles and permissions (RBAC) : Step-by-Step Guide

Get updates directly to your inbox.

Join 500+ developers getting updates on Laravel & Next.js tips. No spam,
unsubscribe anytime.


Share this article:

208 views