top of page

Posts

Automate UnitTest with GitHub Actions




Continuing from the previous article, I will keep talking about GitHub Actions. Today’s topic is "Automating PHP UnitTest with GitHub Actions!"


Note


In this article, we use Laravel environment built by Docker.


|

|- docker

|   |- mysql

|   |  |- Dockerfile

|   |  |- my.cnf

|   |

|   |- php

|     |- Dockerfile

|      |- php.ini

|

|- docker-compose.yml

|

|- src


What is “GitHub Actions”?


This is briefly explained in a previous article, so please refer to that for more information.



Environment


Docker: 23.0.1

Laravel: 10.x

PHP: 8.2

MySQL: 8.0


Code


The code to be used this time is as follows and is written in "/.github/workflows/unitTest.yml".


name: UnitTest

on:

  push:

 

jobs:

  unitTest:

    runs-on: ubuntu-latest

 

    steps:

    - name: Check out code

      uses: actions/checkout@3.6.0

 

    - name: Set up Docker Compose

      run: docker compose up -d

      working-directory: ${{ github.workspace }}  # Specify the root directory of the repository

 

    - name: composer install

      run: docker compose exec -i -t php-fpm composer install

 

    - name: thirty seconds sleep

      run: sleep 30

 

    - name: Run PHPUnit

      run: docker compose exec -i -t php-fpm vendor/bin/phpunit


Based on the above code, I will break down and explain the mechanism for automating UnitTest.


I will also reiterate what was explained in the previous article about Larastan.


name:


This is the field where you specify the name of the workflow that will be registered in GitHub Actions.

By providing a name here, it will be displayed in the GitHub Actions dashboard.

In this instance, I have registered the name as "UnitTest."

However, if you don't specify a name, the workflow will be registered with the relative path of the file. In this case, it would be registered as "./.github/workflows/UnitTest.yml."


on:


You can determine which action triggers the execution of the workflow. 

This is called a "trigger". 

In the code we are using this time, I have set "Push to GitHub" as the trigger to run UnitTest. 

In addition to the push timing, you can also set triggers in detail, such as triggering when a pull request is created, or only when a specified branch is pushed.


jobs:


This section is dedicated to writing the processes to be executed within the workflow.

For example, to run UnitTest, you need execution commands such as starting Docker and installing composer.

This section is meant to detail such operations.


unitTest:


The creation of a job and the job name.

You can use the "name" key in a job to set the name that will be displayed in the GitHub UI. However, if no configuration is provided, it will be replaced with the job's name.


runs-on:


This defines the type of virtual machine.

To configure the virtual machine, you can choose from the following three options:

  • GitHub-hosted Runner

  • Runner Larger Than GitHub-hosted Runner

  • Self-hosted Runner

This time I will use the first option, GitHub-hosted Runner (referred to as "runner").

Within the runner, there are several types available, and you can choose from Linux, Windows, and macOS.

Generally, I think there is no problem with using Linux, but please select according to your specific environment.

For this instance, I am using the latest version of Ubuntu as the virtual machine.


uses:


It specifies the action to be executed.

The action being used here is a module called "actions/checkout".

Its role is to be used to retrieve the code from the repository.


run: (the first one)


Here, the command docker compose up -d is being executed.

Since this time we will be building an environment using docker, we will start docker here.


working-directory:


This section specifies the working directory for the task.

For this instance, I am using  ${{ github.workspace }}, which allows obtaining the absolute path from the default working directory.

And docker is started using the obtained path.


run: (the second one)


The second "run" block installs composer. 

The command being used is docker compose exec -i -t php composer install. 

You will notice that "php" code is mentioned in the command, where you can replace it with the name of your PHP container.

By using the above command, we are executing "composer install" within the PHP container.


run: (the third one)


In the third "run" block, we are using the "sleep" command.

The command is sleep 30.

Since MySQL may not have started completely yet after "docker compose up", UnitTest cannot run properly in that situation, so we are delaying the process for 30 seconds.


run: (the forth one)


In the last, fourth "run" block, we are performing UnitTest.

Similar to the second "run" block, the command to execute UnitTest is written inside the PHP container. The command is docker compose exec -i -t php vendor/bin/phpunit. Please replace "php" in this command with the name of your PHP container.

At this point, UnitTest is finally executed.


Summary


How was it?

With GitHub Actions, you can automate various tasks, even though it might be challenging to implement it, once it's done, you will be able to keep increasing your work efficiency!

Thank you very much.


This blog post is translated from a blog post written by Matsuki on our Japanese website Beyond Co..



Comentarios


bottom of page