Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Feature vs Unit Tests: The Difference

Premium
5 min read

In this lesson, let's talk about Unit Tests versus Feature Tests.

A unit is some internal piece of code in your application: usually a function or a class. So, the purpose of Unit tests is to test precisely those units, without loading the full page routes or API endpoints.

Unit tests usually don't use the full feature but instead, test that some internal method returns correct data.

Let me show you a practical example.


New Feature: Service Class

For this example, we want to show additional prices in Euro in the table.

products table with eur price

To show the price in Euro, there is a converter. Imagine a CurrencyService class in the App\Services directory. In the service, there is a function to convert the price.

app/Services/CurrencyService.php:

namespace App\Services;
 
class CurrencyService
{
const RATES = [
'usd' => [
'eur' => 0.98
],
];
 
public function convert(float $amount, string $currencyFrom, string $currencyTo): float
{
$rate = self::RATES[$currencyFrom][$currencyTo] ?? 0;
 
return round($amount * $rate, 2);
}
}

For simplicity, we just fake the conversion rate as a constant. In the real world, of course, there will be...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

IS
Ilya Savianok ✓ Link copied!

Is there a convenient way to test protected and private methods in unit tests? Is it practiced in large applications?