SyntaxStudy
Sign Up
PHP Feature Tests in Laravel
PHP Intermediate 4 min read

Feature Tests in Laravel

Feature Tests

Feature tests send HTTP requests through the full stack and assert on the JSON response, database state, and session.

Example
class PostTest extends TestCase {
    use RefreshDatabase;
    public function test_authenticated_user_can_create_post(): void {
        $user = User::factory()->create();
        $this->actingAs($user)
            ->postJson("/api/posts", ["title" => "Hello", "body" => "World"])
            ->assertCreated()
            ->assertJsonPath("data.title", "Hello");
        $this->assertDatabaseHas("posts", ["title" => "Hello", "user_id" => $user->id]);
    }
}
Pro Tip

Feature tests give the highest confidence because they test the full request cycle without a browser.