Feature tests check if your app works correctly from the user's view. They test multiple parts together, like pages and forms.
Feature tests in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Laravel
public function test_example_feature()
{
$response = $this->get('/some-url');
$response->assertStatus(200);
$response->assertSee('Welcome');
}Feature tests use HTTP methods like get(), post(), put(), delete() to simulate user actions.
Assertions check the response status, content, redirects, and more.
Examples
Laravel
public function test_homepage_loads()
{
$response = $this->get('/');
$response->assertStatus(200);
$response->assertSee('Laravel');
}Laravel
public function test_user_can_register()
{
$response = $this->post('/register', [
'name' => 'Alice',
'email' => 'alice@example.com',
'password' => 'password',
'password_confirmation' => 'password',
]);
$response->assertRedirect('/home');
$this->assertDatabaseHas('users', ['email' => 'alice@example.com']);
}Sample Program
This feature test checks if the login page loads and if a user can log in with correct details.
Laravel
<?php namespace Tests\Feature; use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class LoginTest extends TestCase { use RefreshDatabase; public function test_user_can_view_login_page() { $response = $this->get('/login'); $response->assertStatus(200); $response->assertSee('Login'); } public function test_user_can_login_with_correct_credentials() { // Create a user $user = \App\Models\User::factory()->create([ 'password' => bcrypt('secret123'), ]); $response = $this->post('/login', [ 'email' => $user->email, 'password' => 'secret123', ]); $response->assertRedirect('/home'); $this->assertAuthenticatedAs($user); } }
Important Notes
Use RefreshDatabase trait to reset the database for each test.
Feature tests simulate real user actions by sending HTTP requests.
Run tests with php artisan test to see results.
Summary
Feature tests check app behavior from the user's perspective.
They use HTTP requests and assertions to verify pages and actions.
They help catch bugs before users see them.
Practice
1. What is the main purpose of
Feature tests in Laravel?easy
Solution
Step 1: Understand the role of feature tests
Feature tests simulate user actions and check the app's responses to ensure it works as expected.Step 2: Compare with other test types
Unlike unit tests or migration tests, feature tests focus on user experience and request handling.Final Answer:
To test how the application behaves from the user's perspective -> Option AQuick Check:
Feature tests = user behavior testing [OK]
Hint: Feature tests simulate user requests and responses [OK]
Common Mistakes:
- Confusing feature tests with unit tests
- Thinking feature tests only check database
- Assuming feature tests optimize performance
2. Which of the following is the correct way to start a feature test method in Laravel?
easy
Solution
Step 1: Recall Laravel test method syntax
Laravel feature test methods must be public and start with 'test' or have the @test annotation.Step 2: Identify correct PHP method declaration
Only 'public function testUserCanLogin()' is valid PHP syntax for a test method in Laravel.Final Answer:
public function testUserCanLogin() -> Option CQuick Check:
Test methods are public functions [OK]
Hint: Test methods must be public functions starting with 'test' [OK]
Common Mistakes:
- Omitting 'public' keyword
- Using wrong function declaration syntax
- Using Python style 'def' keyword
3. Given this Laravel feature test code:
What does this test check?
public function testHomePageReturnsOk()
{
$response = $this->get('/');
$response->assertStatus(200);
}What does this test check?
medium
Solution
Step 1: Analyze the HTTP request in the test
The test sends a GET request to '/' (home page).Step 2: Check the assertion
The test asserts the response status is 200, meaning OK.Final Answer:
The home page returns a 200 OK status -> Option BQuick Check:
assertStatus(200) means success [OK]
Hint: assertStatus(200) means page loaded successfully [OK]
Common Mistakes:
- Confusing 200 with 404 or redirect codes
- Assuming JSON response without checking
- Misreading the URL path
4. What is wrong with this Laravel feature test code?
public function testUserRegistration()
{
$response = $this->post('/register', [
'email' => 'user@example.com',
'password' => 'secret',
]);
$response->assertRedirect('/home');
}medium
Solution
Step 1: Understand Laravel's CSRF protection
Laravel requires a CSRF token for POST requests to prevent attacks.Step 2: Check test request handling
Feature tests use the 'web' middleware group by default, which includes CSRF protection. The $this->post() method does not automatically include a CSRF token, so the test will fail unless the token is provided or middleware is disabled.Step 3: Identify the actual issue
Since the test does not disable middleware, missing CSRF token causes failure.Final Answer:
Missing CSRF token in the post request -> Option AQuick Check:
POST requests need CSRF token unless middleware disabled [OK]
Hint: Remember CSRF token is needed for POST requests [OK]
Common Mistakes:
- Assuming password must be hashed in test input
- Confusing password and password_confirmation fields
- Ignoring middleware effects on tests
5. You want to test a feature where a logged-in user can update their profile. Which sequence correctly sets up the test in Laravel feature tests?
hard
Solution
Step 1: Create a user to simulate logged-in state
First, create a user instance in the database for authentication.Step 2: Use actingAs() to simulate logged-in user
Use $this->actingAs($user) to run requests as that user.Step 3: Send POST request to update profile route
Send the update request while acting as the user.Step 4: Assert the response redirects as expected
Check that the response redirects to the correct page after update.Final Answer:
Create user -> Act as user -> Send POST request to update route -> Assert redirect -> Option DQuick Check:
Setup user then act as user before request [OK]
Hint: Create user first, then act as user before request [OK]
Common Mistakes:
- Sending request before acting as user
- Not creating user before acting as user
- Asserting redirect before sending request
