DEV Community

Dennis Tabaldo
Dennis Tabaldo

Posted on

Laravel Testing Environment Setup

This guide explains how to properly configure a testing environment in Laravel using .env.testing and phpunit.xml.

๐Ÿ“Œ Overview

When running automated tests, you should isolate your testing environment from development and production. This prevents accidental data loss and improves test reliability.

โš™๏ธ 1. Create .env.testing

Create a .env.testing file in the root of your Laravel project:

APP_NAME=laravel
APP_ENV=local
APP_KEY= # application key
APP_DEBUG=true
APP_URL=http://laravel.test

APP_LOCALE=en
APP_FALLBACK_LOCALE=en
APP_FAKER_LOCALE=en_US

APP_MAINTENANCE_DRIVER=file

BCRYPT_ROUNDS=12

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= # your testing database (e.g., laravel-testing)
DB_USERNAME=root
DB_PASSWORD=

SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_ENCRYPT=true
SESSION_PATH=/
SESSION_DOMAIN=null

Enter fullscreen mode Exit fullscreen mode

โœ… Notes
Use a separate database for testing.
Never reuse your development or production database.

๐Ÿงช 2. Configure phpunit.xml

Update your phpunit.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory>tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory>tests/Feature</directory>
        </testsuite>
    </testsuites>

    <source>
        <include>
            <directory>app</directory>
        </include>
    </source>

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="APP_MAINTENANCE_DRIVER" value="file"/>
        <env name="BCRYPT_ROUNDS" value="4"/>

        <env name="BROADCAST_CONNECTION" value="null"/>
        <env name="CACHE_STORE" value="array"/>
        <env name="DB_CONNECTION" value="mysql"/>
        <!-- <env name="DB_DATABASE" value="your-testing-db"/> -->
        <env name="DB_URL" value=""/>

        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>

        <env name="PULSE_ENABLED" value="false"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
        <env name="NIGHTWATCH_ENABLED" value="false"/>
    </php>
</phpunit>

Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ 3. Run Tests

Before running tests:

Ensure your testing database exists
Run migrations if needed

Then execute:

php artisan test

Enter fullscreen mode Exit fullscreen mode

โšก Tips
Isolation: APP_ENV=testing ensures .env.testing is used.
Performance:
CACHE_STORE=array
SESSION_DRIVER=array
QUEUE_CONNECTION=sync
Faster Hashing:
Reduce BCRYPT_ROUNDS during testing
Disable Extras:
Disable tools like Telescope, Pulse, etc., to speed up tests

โœ… Summary

With this setup:

Tests run faster โšก
Data remains safe ๐Ÿ”’
Environment stays isolated ๐Ÿงช

Top comments (0)