title: Laravel Testing Environment Setup
published: true
description:
tags: #test #laravel #phpunit
published_at: 2026-03-26 14:38 +0000
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
✅ Notes
Use a separate database for testing.
Never reuse your development or production database.
- 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>
- Run Tests
Before running tests:
Ensure your testing database exists
Run migrations if needed
Then execute:
php artisan test
⚡ 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