DEV Community

Al Shahriar
Al Shahriar

Posted on

Feature Test vs Unit Test: When to Use Which?

Testing is not just about finding bugs — it’s about building confidence in your code.
As developers, we often hear “Unit Test” and “Feature Test”.
But what’s the difference, and when should you use each?

Unit Test

Purpose: Test small, isolated pieces of code (functions, classes) without dependencies.
Speed: Super fast – no DB, no API calls.
Best for: Business logic, utility functions, services.

Example:

public function test_discount_calculation() { $service = new DiscountService(); $result = $service->calculate(1000, 10); $this->assertEquals(900, $result); } 
Enter fullscreen mode Exit fullscreen mode

Think of it as testing a single brick, not the whole building.

Feature Test
Purpose: Test an entire feature end-to-end — from request → controller/service → database → response.
Speed: Slower – interacts with DB, queues, or APIs.
Best for: Verifying complete workflows like “User Registration”, “Checkout”, “Password Reset”.

Example:

public function test_user_registration() { $response = $this->postJson('/api/register', [ 'name' => 'John Doe', 'email' => 'john@example.com', 'password' => 'Password@123', 'password_confirmation' => 'Password@123', ]); $response->assertStatus(201); $this->assertDatabaseHas('users', ['email' => 'john@example.com']); } 
Enter fullscreen mode Exit fullscreen mode

Think of it as testing the whole building — from the foundation to the roof.

Top comments (0)