Tests are run using 'php artisan test' or './vendor/bin/phpunit'. Can filter tests using --filter flag. Support parallel testing with --parallel option. Generate coverage reports with --coverage flag.
Use get(), post(), put(), patch(), delete() methods in tests. Can chain assertions like ->assertOk(), ->assertRedirect(). Submit forms using call() method with request data.
Tinker is an REPL for Laravel. Access using 'php artisan tinker'. Test code, interact with models, execute queries interactively. Useful for debugging and exploring application state.
Use Mockery for mocking objects. Mock facades using Facade::mock(). Bind mocks in service container. Support partial mocks and spy objects. Verify mock expectations.
Use assertPushed(), assertNotPushed() for job verification. Test job execution and chain handling. Verify job delays and retries. Support queue-specific assertions.
Use withSession() method. Test session data persistence. Verify flash messages. Support session regeneration. Test session expiration.
Measure application performance metrics. Test response times and throughput. Profile database queries. Monitor memory usage. Implement load testing.
Test security vulnerabilities. Implement penetration testing. Verify authentication security. Test authorization rules. Check input validation.
PHPUnit is the default testing framework in Laravel. It provides a suite of tools for writing and running automated tests. Laravel extends PHPUnit with additional assertions and helper methods for testing applications.
Assertions verify expected outcomes in tests. Common assertions include assertTrue(), assertEquals(), assertDatabaseHas(). Laravel adds web-specific assertions like assertStatus(), assertViewIs(), assertJson().
Database testing uses RefreshDatabase or DatabaseTransactions traits. Tests run in transactions to prevent test data persistence. Use factories to generate test data. Assert database state using assertDatabaseHas().
Use .env.testing file for test environment. Configure test database, mail settings, queues. Use config:clear before testing. Support different configurations per test suite.
Use json() method for API requests. Assert response structure, status codes. Test authentication tokens. Verify API rate limiting. Support API versioning in tests.
Use Cache::fake(). Test cache storage and retrieval. Verify cache tags and expiration. Support different cache drivers. Test cache clearing.
Organize tests into suites. Configure suite-specific setup. Handle dependencies between suites. Support parallel suite execution. Implement suite-level fixtures.
Set up CI/CD pipelines. Configure test automation. Handle environment setup. Support different test stages. Implement test reporting.
Generate API documentation from tests. Verify API specifications. Test API versioning. Support OpenAPI/Swagger integration. Implement documentation automation.
Test application under heavy load. Verify system stability. Monitor resource usage. Implement crash recovery. Handle concurrent requests.
Tests are created using 'php artisan make:test TestName'. Two types available: Feature tests (--test suffix) and Unit tests (--unit flag). Tests extend TestCase class and are stored in tests directory.
Factories generate fake data for testing using Faker library. Created with 'php artisan make:factory'. Define model attributes and relationships. Support states for different scenarios.
Use actingAs() for authenticated requests. Test login, logout, registration flows. Verify middleware protection. Test password reset functionality. Support multiple guards in tests.
Use Event::fake() to mock events. Assert event dispatch and handling. Test event listeners in isolation. Verify event payload. Support conditional event assertions.
Use Mail::fake() for mail testing. Assert mail sent and contents. Test mail templates. Verify attachments and recipients. Support markdown mail testing.
Use UploadedFile class for fake files. Test file validation and storage. Verify file processing. Support multiple file uploads. Test file deletion.
Use Laravel Dusk for browser testing. Test JavaScript interactions. Support multiple browsers. Handle authentication in browser tests. Test file downloads.
Use mutation testing frameworks. Verify test coverage quality. Identify weak test cases. Support automated mutation analysis. Handle false positives.
Maintain test suite for existing features. Automate regression checks. Handle backward compatibility. Support feature flags in tests. Implement version testing.
Feature tests focus on larger portions of code and test application behavior from user perspective. Unit tests focus on individual classes or methods in isolation. Feature tests typically test HTTP requests, while unit tests verify specific functionality.
Use Notification::fake(). Assert notification sending and channels. Test notification content. Verify notification queuing. Support custom channel testing.
Create test-specific seeders. Handle complex data relationships. Support different seeding strategies. Implement seeder factories. Handle large dataset seeding.