Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amitsolanki24_'s avatar

Unit test vs feature test vs pest test

Hey everyone,

When do I use unit and feature test?

And Why we use pest test when there is unit test?

Thank you,

Amit solanki

0 likes
4 replies
tykus's avatar

A unit test tests a unit of code, e.g. a function or class, without touching any other code/dependencies.

A feature test tests more than a single class or function; it tests a feature of the application, e.g in a forum application, it might test the creating a Post from an incoming Request through to persisting a record in the database and any side-effects like events/listeners/notifications.

Pest is a functional abstraction around PHPUnit; it is not comparable to unit or feature tests since you could write either unit or feature test using Pest.

I wouldn't get too worked up about the terminology; just understand that in the context of a Laravel project; Feature tests will boot the framework by extending the Tests\TestCase class, whereas the Unit tests do not. You can get by with mostly Feature tests, dropping into Unit tests only when needed

1 like
amitsolanki24_'s avatar

@tykus okay, suppose i have to test login and register functionality both are smaller unit of code and both are also features.

So where should I write these tests into feature or in unit?

tykus's avatar

@amitsolanki24_ login and registration are Features; for example, testing the Login happy path functionality, you will be testing the following (i) making a POST request to the loginendpoint (ii) locates an existing User record from the database using the credentials provided (iii) creates a database record in the sessions table (iv) the user is authenticated, So, you see it is not a "smaller unit" of code; it needs the framework booted and a database to persist records. A Unit test would have no such external dependencies.

1 like

Please or to participate in this conversation.