Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Writing Feature Tests in Laravel
📖 Scenario: You are building a simple blog application using Laravel. You want to make sure that the homepage loads correctly and shows a welcome message.
🎯 Goal: Create a feature test in Laravel that checks if the homepage loads successfully and contains the text "Welcome to the Blog".
📋 What You'll Learn
Create a feature test class named HomePageTest
Write a test method named test_homepage_loads_successfully
Use Laravel's get method to request the homepage URL '/'
Assert that the response status is 200
Assert that the response contains the text 'Welcome to the Blog'
💡 Why This Matters
🌍 Real World
Feature tests help ensure that your web pages load correctly and show the right content. This prevents bugs and improves user experience.
💼 Career
Writing feature tests is a key skill for Laravel developers. It helps maintain code quality and confidence when making changes.
Progress0 / 4 steps
1
Create the feature test class
Create a feature test class named HomePageTest inside the tests/Feature directory. Use the namespace Tests\Feature and extend the Tests\TestCase class.
Laravel
Hint
Use php artisan make:test HomePageTest to generate the test class automatically.
2
Add the test method
Inside the HomePageTest class, add a public method named test_homepage_loads_successfully that will hold the test logic.
Laravel
Hint
Test methods should start with test_ and be public.
3
Request the homepage and check status
Inside the test_homepage_loads_successfully method, use $this->get('/') to request the homepage. Store the response in a variable named $response. Then assert that the response status is 200 using assertStatus(200).
Laravel
Hint
Use $response = $this->get('/') to get the homepage response.
4
Assert the homepage contains welcome text
Add an assertion to the test_homepage_loads_successfully method that checks if the response contains the text 'Welcome to the Blog' using assertSee('Welcome to the Blog').
Laravel
Hint
Use assertSee to check if the response contains specific text.
Practice
(1/5)
1. What is the main purpose of Feature tests in Laravel?
easy
A. To test how the application behaves from the user's perspective
B. To check the syntax of PHP code
C. To test database migrations only
D. To optimize application performance
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 A
Quick 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
A. public testUserCanLogin()
B. function testUserCanLogin()
C. public function testUserCanLogin()
D. def testUserCanLogin()
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 C
Quick 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:
public function testHomePageReturnsOk()
{
$response = $this->get('/');
$response->assertStatus(200);
}
What does this test check?
medium
A. The home page returns a 404 error
B. The home page returns a 200 OK status
C. The home page redirects to login
D. The home page returns JSON data
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 B
Quick 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
A. Missing CSRF token in the post request
B. Password should be hashed before sending
C. Password field should be 'password_confirmation' instead of 'password'
D. The test method is missing the 'public' keyword
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 A
Quick 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
A. Assert redirect -> Create user -> Send POST request -> Act as user
B. Send POST request to update route -> Create user -> Assert redirect -> Act as user
C. Act as user -> Assert redirect -> Create user -> Send POST request
D. Create user -> Act as user -> Send POST request to update route -> Assert redirect
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 D
Quick Check:
Setup user then act as user before request [OK]
Hint: Create user first, then act as user before request [OK]