Richard C.
—You can automatically run your unit tests when committing your Laravel project to GitHub by using a GitHub Actions workflow. The workflow is configured in a .yaml
file in your repository’s .github/workflows
folder.
Your unit tests will need a database to run against. How can I set this up in a GitHub workflow?
GitHub includes a MySQL and PostgreSQL database in their test runner. Let’s look at how to use it.
To run a GitHub Action when you commit your code, create the file .github/workflows/test.yaml
.
Insert the following content:
name: Test Laravel on: push: branches: - main workflow_dispatch:
The code above sets the name of the workflow that will display in the Actions tab of your GitHub repository, then sets two events on which the workflow will run:
Next, add the code below which specifies what will run when these events trigger:
jobs: run-tests: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Install PHP latest run: | sudo apt remove php -y sudo apt update sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt -y install php sudo apt update sudo apt install php-curl php-xml php-mbstring php-mysql -y - name: Start Mariadb run: | sudo systemctl start mysql.service mysql -uroot -proot -e 'CREATE DATABASE IF NOT EXISTS laravel;' - name: Install Composer dependencies run: composer update - name: Run unit tests run: | php artisan migrate php artisan test
The only job in this workflow is run-tests
set to run on a Linux machine. There are five steps:
GitHub provides a test runner environment based on Ubuntu Linux with some extra features. For a full overview, see this documentation.
Note that the MySQL username and password are root
. Set these in your .env
file to run the test in an experimental project, or use GitHub secrets when running in production (where you don’t want to commit your .env
file secrets to version control).
The Ubuntu 22.04 image uses PHP 8.1, so in this script, we install the latest available PHP version in the PPA ppa:ondrej/php
manually (8.3 at the time of writing). You could also install PHP by searching for a PHP Action either from GitHub, or on the Actions Marketplace. In the configuration, you can also use an Action for a different database like PostgreSQL.
Save the test.yaml
file, commit it, and push it to GitHub. On the Actions tab of your repository you can see your tests run.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.