Overview
Laravel Breeze API uses Pest PHP for testing. Pest provides an elegant testing syntax built on top of PHPUnit, making tests more readable and easier to write.Test Configuration
PHPUnit Configuration
File:phpunit.xml
- Database: SQLite in-memory for fast, isolated tests
- Cache: Array driver (no persistence)
- Mail: Array driver (captures emails without sending)
- Queue: Sync driver (processes jobs immediately)
- Session: Array driver (in-memory sessions)
- Bcrypt Rounds: Reduced to 4 for faster password hashing
Running Tests
Run All Tests
Run with Composer
Run Specific Test File
Run Specific Test
Run with Coverage
Run in Parallel
Test Structure
Test Directories
- Feature Tests: Test complete features and API endpoints
- Unit Tests: Test individual classes and methods
Authentication Tests
AuthenticationTest
File:tests/Feature/Auth/AuthenticationTest.php
- Login with valid credentials
- Login rejection with invalid password
- Logout functionality
- Session management
RegistrationTest
File:tests/Feature/Auth/RegistrationTest.php
- User registration
- Automatic login after registration
- Password confirmation validation
Pest Testing Syntax
Basic Test Structure
Using Expect (Pest Style)
Common Assertions
Authentication Assertions
Response Assertions
Database Assertions
Writing Custom Tests
Feature Test Example
Unit Test Example
Testing Email
Email Verification Test
Password Reset Email Test
Testing Validation
Testing Rate Limiting
Testing API with CSRF
Best Practices
Use Factories
Always use factories to create test data:Keep Tests Isolated
Each test should be independent:Test One Thing
Each test should verify one specific behavior:Use Descriptive Test Names
Continuous Integration
Add to your CI pipeline:.github/workflows/tests.yml
Next Steps
API Routes
Review API endpoints to test
Controllers
Understand controller logic