0
0
Laravelframework~30 mins

Feature tests in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use assertSee to check if the response contains specific text.