Bird
Raised Fist0
Laravelframework~10 mins

Feature tests in Laravel - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - Feature tests
Write Feature Test Method
↓
Set Up Test Environment
↓
Send HTTP Request
↓
Check Response Status
↓
Assert Response Content
↓
Test Pass or Fail
Feature tests in Laravel simulate user actions by sending HTTP requests and checking responses step-by-step.
Execution Sample
Laravel
public function test_homepage_shows_welcome()
{
    $response = $this->get('/');
    $response->assertStatus(200);
    $response->assertSee('Welcome');
}
This test checks if the homepage loads successfully and shows the word 'Welcome'.
Execution Table
StepActionInput/ConditionResultNext Step
1Call test methodtest_homepage_shows_welcome()Test startsSend HTTP GET request '/'
2Send HTTP GET requestURL: '/'Response received with status 200Assert status is 200
3Assert statusResponse status == 200PassAssert response contains 'Welcome'
4Assert contentResponse body contains 'Welcome'PassTest passes
5Test endsAll assertions passedSuccessEnd
💡 Test ends after all assertions pass or fail
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
$responsenullHTTP response object with status 200Same response objectSame response objectTest passed
Key Moments - 2 Insights
Why do we call $this->get('/') instead of directly calling the controller?
Because feature tests simulate real HTTP requests to the app, testing the full request lifecycle including middleware and routing, as shown in execution_table step 2.
What happens if assertStatus(200) fails?
The test stops and fails immediately at step 3 in the execution_table, so later assertions like assertSee won't run.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response status after sending the GET request?
A500
B200
C404
D302
💡 Hint
Check row 2 under 'Result' column in execution_table
At which step does the test check if the page contains the word 'Welcome'?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for the assertSee call
If the response status was 404 instead of 200, what would happen?
ATest would fail at assertStatus step
BTest would pass anyway
CTest would fail at assertSee step
DTest would continue but skip assertions
💡 Hint
Refer to key_moments about assertStatus failure and execution_table step 3
Concept Snapshot
Feature tests in Laravel simulate HTTP requests to your app.
Use $this->get('/url') to send requests.
Check response status with assertStatus().
Check page content with assertSee().
Tests fail immediately if assertions fail.
This tests the app end-to-end like a user.
Full Transcript
Feature tests in Laravel let you check how your app responds to HTTP requests. You write a test method that sends a request using $this->get() or other HTTP verbs. Then you check the response status with assertStatus() to make sure the page loaded correctly. Next, you can check if the response contains certain text with assertSee(). If any assertion fails, the test stops and reports failure. This way, feature tests simulate real user actions and test the full app flow including routing and middleware.

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

  1. 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.
  2. Step 2: Compare with other test types

    Unlike unit tests or migration tests, feature tests focus on user experience and request handling.
  3. Final Answer:

    To test how the application behaves from the user's perspective -> Option A
  4. 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

  1. Step 1: Recall Laravel test method syntax

    Laravel feature test methods must be public and start with 'test' or have the @test annotation.
  2. Step 2: Identify correct PHP method declaration

    Only 'public function testUserCanLogin()' is valid PHP syntax for a test method in Laravel.
  3. Final Answer:

    public function testUserCanLogin() -> Option C
  4. 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

  1. Step 1: Analyze the HTTP request in the test

    The test sends a GET request to '/' (home page).
  2. Step 2: Check the assertion

    The test asserts the response status is 200, meaning OK.
  3. Final Answer:

    The home page returns a 200 OK status -> Option B
  4. 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

  1. Step 1: Understand Laravel's CSRF protection

    Laravel requires a CSRF token for POST requests to prevent attacks.
  2. 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.
  3. Step 3: Identify the actual issue

    Since the test does not disable middleware, missing CSRF token causes failure.
  4. Final Answer:

    Missing CSRF token in the post request -> Option A
  5. 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

  1. Step 1: Create a user to simulate logged-in state

    First, create a user instance in the database for authentication.
  2. Step 2: Use actingAs() to simulate logged-in user

    Use $this->actingAs($user) to run requests as that user.
  3. Step 3: Send POST request to update profile route

    Send the update request while acting as the user.
  4. Step 4: Assert the response redirects as expected

    Check that the response redirects to the correct page after update.
  5. Final Answer:

    Create user -> Act as user -> Send POST request to update route -> Assert redirect -> Option D
  6. Quick 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