All Testing lessons

HTTP feature tests

5 min read

HTTP tests boot the full framework but skip the network — they're fast and end-to-end at the same time.

// PHPUnit style
public function test_guest_can_view_packages(): void
{
    $this->get('/')
         ->assertOk()
         ->assertSee('Open-source tools');
}

public function test_admin_can_publish_post(): void
{
    $admin = User::factory()->admin()->create();

    $response = $this->actingAs($admin)->post('/posts', [
        'title' => 'Hello',
        'body'  => 'World',
    ]);

    $response->assertRedirect('/posts');
    $this->assertDatabaseHas('posts', ['title' => 'Hello']);
}

Pest style

it('lets a guest see the home page', function () {
    get('/')->assertOk()->assertSee('Open-source tools');
});

Pest is just a thin layer over PHPUnit — same assertions, terser syntax.

Useful assertions

  • assertOk, assertStatus, assertRedirect, assertJson, assertJsonPath
  • assertSee, assertSeeText, assertDontSee
  • assertDatabaseHas, assertDatabaseCount, assertDatabaseMissing
  • assertViewIs, assertViewHas