Performance: HTTP test assertions
This concept affects the speed and reliability of backend test execution, indirectly impacting developer feedback loops and deployment confidence.
Jump into concepts and practice - no test required
public function testExample() {
$response = $this->get('/api/data');
$response->assertStatus(200)
->assertJson(['key' => 'value'])
->assertSee('Some text')
->assertJsonStructure(['data' => ['id', 'name']]);
}public function testExample() {
$response = $this->get('/api/data');
$response->assertStatus(200);
$response->assertJson(['key' => 'value']);
$response->assertSee('Some text');
$response->assertJsonStructure(['data' => ['id', 'name']]);
}| Pattern | CPU Usage | Test Runtime | Redundancy | Verdict |
|---|---|---|---|---|
| Multiple separate assertions | High | Longer | High | [X] Bad |
| Chained assertions | Low | Shorter | Low | [OK] Good |
assertStatus$response->assertStatus(200); confirms the response was successful (OK).assertRedirect() to check if the response redirects to a given URL.$response->assertRedirect('/dashboard'); which asserts a redirect to '/dashboard'.$response = $this->get('/welcome');
$response->assertStatus(200);
$response->assertSee('Hello World');assertStatus(200)$response = $this->post('/login', ['email' => 'user@example.com', 'password' => 'secret']);
$response->assertRedirect('/dashboard');
$response->assertStatus(200);assertRedirect('/dashboard') expects a 302 status, but assertStatus(200) expects 200, causing failure.assertStatus(201) is needed.assertJson to check JSON contentassertJson(['status' => 'success']) verifies the JSON key and value.assertStatus(201) and assertJson([...]) is valid and checks both conditions.