Feature Tests - Testing HTTP Endpoints
Feature tests make HTTP requests to your application and assert on the response - status codes, JSON structure, view data, redirects, session data, and database state. They test the full request lifecycle including middleware, validation, and database operations.
Feature Test Examples
// tests/Feature/PostControllerTest.php
uses(RefreshDatabase::class);
it('displays the post index page', function () {
Post::factory(5)->published()->create();
$this->get('/posts')
->assertStatus(200)
->assertViewIs('posts.index')
->assertViewHas('posts');
});
it('allows authenticated users to create posts', function () {
$user = User::factory()->create();
$this->actingAs($user)
->post('/posts', [
'title' => 'My New Post',
'body' => 'This is the post body content.',
])
->assertRedirect()
->assertSessionHas('success');
$this->assertDatabaseHas('posts', [
'title' => 'My New Post',
'user_id' => $user->id,
]);
});
it('validates required fields', function () {
$user = User::factory()->create();
$this->actingAs($user)
->post('/posts', [])
->assertSessionHasErrors(['title', 'body']);
});
it('prevents guests from creating posts', function () {
$this->post('/posts', ['title' => 'Test'])
->assertRedirect('/login');
});
it('allows post owner to delete', function () {
$post = Post::factory()->create();
$this->actingAs($post->user)
->delete("/posts/{$post->id}")
->assertRedirect('/posts');
$this->assertSoftDeleted('posts', ['id' => $post->id]);
});Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Tip
Tip
Practice Feature Tests Testing HTTP Endpoints in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Technical diagram.
Practice Task
Note
Practice Task - (1) Write a working example of Feature Tests Testing HTTP Endpoints from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Feature Tests Testing HTTP Endpoints is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.
Key Takeaways
- Feature tests make HTTP requests to your application and assert on the response - status codes, JSON structure, view data, redirects, session data, and database state.
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class