I want to run my unit tests through my yml file, but they fail because they must be run on Mysql
I think something is missing.
here is yaml file:
steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '7.4|8.0'
- uses: actions/checkout@v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Optimize Project
run: php artisan optimize:clear
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: mysql
DB_HOST: 127.0.0.1
DB_PORT: 3306
DB_DATABASE: database_name
DB_USERNAME: root
DB_PASSWORD:
run: |
php artisan migrate
php artisan test
and here is phpunit.xml file:
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
<!-- <server name="DB_DATABASE" value=":memory:"/> -->
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>
This example shows an example of adding MySQL into into your GitHub action, so the database is available:
Your GitHub actions yaml needs a services
section:
services:
# mysql-service Label used to access the service container
mysql:
# Docker Hub image (also with version)
image: mysql:5.7
env:
## Accessing to Github secrets, where you can store your configuration
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test_db
## map the "external" 3306 port with the "internal" 3306
ports:
- 3306:3306
# Set health checks to wait until mysql database has started (it takes some seconds to start)
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
The env
you can set with your GH Actions command that needs database access can include:
env:
DB_CONNECTION: mysql
DB_DATABASE: test_db
DB_USER: root
DB_PASSWORD: root
You may want to check out Chipper CI, which does a lot of this work for you for testing Laravel apps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With