Now Reading
Laravel Automated Testing with Github Actions

Laravel Automated Testing with Github Actions

Have you ever planned to test your code automatically when you push it to Github branch or doing anything like that ? We can do that with the help of something called Github Actions.

You can run automated testing or pushing code to your production or deployment server or run npm commands when you do that. Here in this tutorial I will show you how to run php unit test in Laravel whenever you push you code to Github

Github Actions page

So here comes the steps to do it.

Github has something called workflows which come with default set of code to run Laravel testing. You can just click on *Set up this workflow* in Laravel section. This will create a boilerplate with all the necessary code to run a PHP unit test for your laravel project.
Note: This boilerplate comes with the code for testing Laravel project with SQLite integration. So we are making changes in the way to test the application with MySql integration.

name: Laravel

on:
  push:
    branches: [ v0.1 ]
  pull_request:
    branches: [ v0.1 ]

jobs:
  laravel-tests:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Copy .env
      run: php -r "file_exists('.env') || copy('.env.example', '.env');"
    - name: Install Dependencies
      run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
    - name: Generate key
      run: php artisan key:generate
    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache
    - name: Create Database
      run: |
        mkdir -p database
        touch database/database.sqlite
    - name: Execute tests (Unit and Feature tests) via PHPUnit
      env:
        DB_CONNECTION: sqlite
        DB_DATABASE: database/database.sqlite
      run: vendor/bin/phpunit

This is the default script which gets generated. If you check this out there is no services added like mysql to process testing with that. So to do that we are adding few more lines in the YML file.

services:
      mysql:
        image: mysql:5.7
        env:
            MYSQL_ALLOW_EMPTY_PASSWORD: yes
            MYSQL_DATABASE: gigcodes_test
        ports:
            - 3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

This is the extra code for adding mysql to the services that are used for github actions.
Note: Here you could find *MYSQL_ALLOW_EMPTY_PASSWORD: yes* line is added which means the database has an empty password.

Sometimes you might test the application on the local machine and you wanna make sure that Github action has the same mysql password so that you don’t wanna keep changing the .env file. *You can always create .env.testing file to make the process easier*.

To add a password to this we are adding some more lines.

services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_USER: admin
          MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}
          MYSQL_ROOT_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}
          MYSQL_DATABASE: gigcodes_test
        ports:
          - 3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

There is something different in the script right ?. *${{ secrets.MYSQL_PASSWORD }}*. This is called Github secrets. If you are a open source developer and I am pretty sure know that you won’t wish to share your DB password to the world. So you can use this.

See Also
changing mysql 8 root password

Page in Github to add secrets.

So the final script after making all the changes

name: Laravel

on: ['push']

jobs:
  laravel-tests:

    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_USER: admin
          MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}
          MYSQL_ROOT_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}
          MYSQL_DATABASE: link_agent_test
        ports:
          - 3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
      - uses: actions/checkout@v2
      - name: Copy .env
        run: php -r "file_exists('.env') || copy('.env.example', '.env');"
      - name: Install Dependencies
        run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
      - name: Generate key
        run: php artisan key:generate
      - name: Directory Permissions
        run: chmod -R 777 storage bootstrap/cache
      - name: Execute tests (Unit and Feature tests) via PHPUnit
        env:
          DB_CONNECTION: mysql
          DB_DATABASE: gigcodes_test
          DB_PORT: ${{ job.services.mysql.ports[3306] }}
        run: vendor/bin/phpunit

This will run Github actions whenever you push code to the codebase. You might have a question like where this testing actually runs coz you have not added any server configurations and all. This runs on Github shared servers based on the configuration you add in the YAML file.

That’s all now your testing is automated. This might be helpful in lot of areas when you are developing something cool. If you have any queries at that in the comments. We have a forum which is pretty new. Would like to see some active developers over there too.

Thanks for taking a look.
#stayhome #staysafe

Happy Coding­čśÄ

View Comments (0)

Leave a Reply

Your email address will not be published.

Scroll To Top