Test Environment Setup for Web Projects
A test environment is an isolated setup with its own database, configuration, and state. Without proper setup, tests become unstable, slow, and interfere with each other.
Test Environment Layers
Unit tests → In-memory / mocks → no external dependencies
Integration → Test database (SQLite or Docker PostgreSQL)
E2E / Browser → Staging environment or local Docker Compose
Load tests → Dedicated staging (isolated from main)
Docker Compose for Test Environment
# docker-compose.test.yml
services:
app:
build:
context: .
target: test
environment:
APP_ENV: testing
DB_HOST: db
DB_DATABASE: testdb
REDIS_HOST: redis
QUEUE_CONNECTION: sync # queues sync in tests
MAIL_MAILER: array # intercept emails to array
CACHE_DRIVER: array # cache in memory
depends_on:
db: { condition: service_healthy }
redis: { condition: service_healthy }
db:
image: postgres:16-alpine
environment:
POSTGRES_DB: testdb
POSTGRES_USER: test
POSTGRES_PASSWORD: test
healthcheck:
test: ["CMD-SHELL", "pg_isready -U test"]
interval: 5s
timeout: 3s
retries: 5
tmpfs:
- /var/lib/postgresql/data # DB in RAM — faster
redis:
image: redis:7-alpine
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
Laravel Configuration for Tests
// phpunit.xml
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="MAIL_MAILER" value="array"/>
</php>
// Basic TestCase with transactions
abstract class TestCase extends BaseTestCase
{
use RefreshDatabase; // rollback DB after each test
protected function setUp(): void
{
parent::setUp();
$this->withoutVite(); // don't run Vite in tests
$this->seed(TestDatabaseSeeder::class);
}
}
Test Isolation
Transactions (fast, but not for HTTP-client tests):
// DatabaseTransactions trait — rollback after each test
use DatabaseTransactions;
Migrations (slower, but more reliable):
// RefreshDatabase trait — recreate DB before each test
use RefreshDatabase;
Mocking External Services:
// Don't make real HTTP requests in tests
Http::fake([
'api.stripe.com/*' => Http::response(['id' => 'pi_test_123'], 200),
'api.sendgrid.com/*' => Http::response(['message_id' => 'test'], 202),
'*' => Http::response(['error' => 'Unexpected request'], 500),
]);
// Intercept email
Mail::fake();
// Verify
Mail::assertSent(OrderConfirmationMail::class, fn($mail) =>
$mail->hasTo('[email protected]')
);
Test Data Factories
// database/factories/UserFactory.php
class UserFactory extends Factory
{
public function definition(): array
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => Hash::make('password'),
];
}
public function admin(): static
{
return $this->afterCreating(fn(User $user) =>
$user->assignRole('admin')
);
}
public function unverified(): static
{
return $this->state(['email_verified_at' => null]);
}
}
// Usage in tests
$user = User::factory()->admin()->create();
$users = User::factory()->count(10)->create();
CI/CD — GitHub Actions
name: Tests
on: [push, pull_request]
jobs:
unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with: { php-version: '8.3', extensions: 'sqlite3' }
- run: composer install --no-interaction
- run: php artisan test --parallel --testsuite=Unit
integration:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: testdb
POSTGRES_PASSWORD: test
ports: ['5432:5432']
options: --health-cmd pg_isready --health-interval 5s
env:
DB_CONNECTION: pgsql
DB_HOST: localhost
DB_DATABASE: testdb
DB_PASSWORD: test
steps:
- uses: actions/checkout@v4
- run: composer install
- run: php artisan test --testsuite=Feature
Test Database Seeder
class TestDatabaseSeeder extends Seeder
{
public function run(): void
{
// Minimal data set for tests
Role::create(['name' => 'admin']);
Role::create(['name' => 'user']);
Category::factory(5)->create();
Product::factory(20)->create();
}
}
Implementation Timeline
Setting up a test environment from scratch (Docker + CI + factories): 3–5 days.







