0
0
Laravelframework~8 mins

Feature tests in Laravel - Performance & Optimization

Choose your learning style9 modes available
Performance: Feature tests
MEDIUM IMPACT
Feature tests impact the overall development feedback loop and CI/CD pipeline speed, affecting how quickly developers can detect issues and deploy.
Testing user interactions and HTTP requests in a Laravel app
Laravel
public function testUserCanCreatePost() {
    $postService = $this->mock(PostService::class);
    $postService->shouldReceive('create')->once()->andReturn(new Post(['title' => 'Test']));
    $user = User::factory()->make();
    $this->actingAs($user);
    $response = $this->post('/posts', ['title' => 'Test', 'body' => 'Content']);
    $response->assertStatus(201);
}
Mocks service layer to avoid database and external dependencies, speeding up test execution.
📈 Performance GainReduces test runtime by 50-70%, enabling faster feedback.
Testing user interactions and HTTP requests in a Laravel app
Laravel
public function testUserCanCreatePost() {
    $user = User::factory()->create();
    $this->actingAs($user);
    $response = $this->post('/posts', ['title' => 'Test', 'body' => 'Content']);
    $response->assertStatus(201);
    $this->assertDatabaseHas('posts', ['title' => 'Test']);
}
This test hits the database and runs full HTTP requests, making it slow and resource-heavy.
📉 Performance CostBlocks test suite for 200-500ms per test; slows CI pipeline.
Performance Comparison
PatternDatabase CallsExternal RequestsTest RuntimeVerdict
Full HTTP request with DBMultipleNone or someHigh (200-500ms+)[X] Bad
Mocked services, no DBNoneNoneLow (50-150ms)[OK] Good
Rendering Pipeline
Feature tests do not directly affect browser rendering but influence developer productivity and deployment speed by controlling test execution time.
Test Execution
CI/CD Pipeline
⚠️ BottleneckDatabase and external service calls during tests
Optimization Tips
1Avoid hitting the real database in feature tests to reduce runtime.
2Use mocking to isolate tests from external services.
3Keep feature tests focused and fast to speed up developer feedback.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue with feature tests that hit the database directly?
AThey cause browser layout shifts
BThey reduce CSS selector efficiency
CThey increase test runtime and slow down the feedback loop
DThey block JavaScript execution in the browser
DevTools: PHPUnit with Xdebug Profiler or Laravel Telescope
How to check: Run tests with Xdebug profiler enabled or use Laravel Telescope to monitor database queries during tests.
What to look for: Look for long test runtimes and excessive database queries indicating slow tests.