Sunday, March 2, 2025

How to Create Automated Testing on Laravel only with Github Action/CI-CD


Streamlining the development process is crucial for any project, especially when dealing with the complexities of web applications. Manual testing quickly becomes a bottleneck as features proliferate. This is where Continuous Integration (CI) and Continuous Deployment (CD) shine, automating testing and deployment, saving developers valuable time and reducing the risk of errors. This article provides a comprehensive guide to setting up a robust CI/CD pipeline for your Laravel projects using GitHub Actions. We'll walk you through the entire process, from creating a new Laravel project to monitoring test results directly within your GitHub repository.

This tutorial assumes a basic understanding of Laravel, Git, and the command line. We'll cover setting up a new Laravel project, initializing a Git repository, creating a GitHub Actions workflow file, and interpreting the test results. By the end, you'll be equipped to implement automated testing for your own Laravel applications, ensuring a smoother, more efficient development cycle.

We'll start by creating a fresh Laravel application. Make sure you have Composer and a recent PHP version installed on your system. Open your terminal and execute the following command:

      composer create-project --prefer-dist laravel/laravel my-laravel-project

This command downloads and installs Laravel and its dependencies into a new directory named my-laravel-project. You can, of course, replace my-laravel-project with your preferred project name. After the installation completes, navigate into the project directory:

      cd my-laravel-project

Next, we'll initialize a Git repository to track code changes and enable seamless integration with GitHub. Within your project directory, run these commands:

git init
git add .
git commit -m "Initial commit"
    

These commands initialize a local Git repository, stage all the files, and commit the initial project snapshot. Now, let's connect this local repository to your GitHub repository. First, create a new repository on GitHub. Then, get the repository URL from GitHub. Once you have the URL, use the following command to add the remote repository:

      git remote add origin <your_github_repository_url>

Replace <your_github_repository_url> with the actual URL of your GitHub repository. Now, push your local commits to GitHub:

      git push -u origin main
    

This command pushes your local main branch (or master, depending on your GitHub settings) to the remote repository on GitHub. This sets up the basic version control infrastructure.

The core of our automated testing process lies in the GitHub Actions workflow file. This file defines the steps GitHub Actions will execute whenever code is pushed to your repository. Create a new directory named .github/workflows within your project. Inside this directory, create a YAML file named laravel-test.yml. This file will contain the instructions for our automated testing workflow.

Paste the following code into the laravel-test.yml file:

name: Laravel Test Workflow

on:
  push:
    branches:
      - main #Specify the branch to trigger the workflow

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1' # Use a supported PHP version
          extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv
          coverage: none

      - name: Install dependencies
        run: composer install --no-interaction --no-progress --optimize-autoloader

      - name: Generate application key
        run: php artisan key:generate --show

      - name: Copy environment file
        run: cp .env.example .env

      - name: Database migrations
        run: php artisan migrate --database=testing

      - name: Run tests
        run: php artisan test --testdox

      - name: Upload test results (optional)
        uses: actions/upload-artifact@v3
        with:
          name: test-results
          path: tests/
    

Let's break down this YAML file:

  • name: This line simply names the workflow.

  • on: push: This triggers the workflow whenever code is pushed to the specified branch (main in this case). You can customize this to trigger on other events like pull requests.

  • jobs: test: This defines a job named "test" that will run the testing steps.

  • runs-on: ubuntu-latest: This specifies that the job will run on a Ubuntu virtual machine.

  • steps: This section defines the individual steps within the job. Each step can be a shell command or a reusable action. The steps above cover checking out the code, setting up PHP, installing dependencies, generating an application key, running database migrations (if any), and running the tests using the php artisan test command. The --testdox flag provides a more readable output of the test results. The optional artifact upload step allows you to archive test results for later review.

After adding this file, commit and push the changes to your GitHub repository:

git add .github/workflows/laravel-test.yml
git commit -m "Added GitHub Actions workflow"
git push origin main
    

Once you push these changes, GitHub Actions will automatically start running your workflow. You can monitor the progress and results in the "Actions" tab of your GitHub repository. A successful workflow run will be indicated by a green checkmark, while failures will show a red cross. Detailed logs for each step are available, allowing you to diagnose any issues that may arise.

This CI/CD pipeline ensures that every code change undergoes automated testing, catching potential problems early in the development cycle. By integrating this system into your workflow, you significantly improve code quality and reduce the risk of deploying faulty code to production. This leads to faster development cycles, quicker bug fixes, and a more robust application. The clear, concise logs from GitHub Actions make debugging far simpler, drastically reducing the time spent tracking down errors.

Remember to adapt the PHP version and other configurations within the laravel-test.yml file to match your specific project requirements. By implementing this simple yet effective strategy, you’ll elevate your Laravel development process to a new level of efficiency and reliability.

0 comments:

Post a Comment