Use unit tests when you want fast, isolated tests for internal logic.
Use feature tests when testing real-world flows through your app that may depend on routing, middleware, or the database.
Purpose: Test small, isolated pieces of logic (usually classes or methods).
Located in: tests/Unit
How they work: They don’t load the full Laravel framework or handle things like database, routes, middleware, etc.
Use Case: Testing business logic, helper classes, service classes, etc.
// tests/Unit/PriceCalculatorTest.php
use Tests\TestCase;
class PriceCalculatorTest extends TestCase
{
public function test_discount_is_applied_correctly()
{
$calculator = new \App\Services\PriceCalculator();
$result = $calculator->applyDiscount(100, 10); // 10%
$this->assertEquals(90, $result);
}
}
Purpose: Test the application's full stack (routes, controllers, middleware, database, etc.)
Located in: tests/Feature
How they work: They boot the Laravel framework and simulate real user behavior (like submitting forms or visiting pages).
Use Case: Testing HTTP requests, database interaction, full flow of a feature.
// tests/Feature/UserRegistrationTest.php
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class UserRegistrationTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_register()
{
$response = $this->post('/register', [
'name' => 'John Doe',
'email' => '[email protected]',
'password' => 'password',
'password_confirmation' => 'password',
]);
$response->assertRedirect('/home');
$this->assertDatabaseHas('users', ['email' => '[email protected]']);
}
}