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

Dirk313's avatar

Unit/Feature testing in Laravel 9

Hey all, just a quick question, i want to start writing unit and feature test, and have gone through the documentation but im still not sure what part you test using "Unit Test" and do you test using Future tests, Any advise will really help to get started on doing unit and feature testing . Thank you in advance

0 likes
3 replies
tykus's avatar

A Unit Test tests a unit of logic; independent of other code/framework (for this reason it extends the base PHPUnit TestCase class. Whenever you want to leverage the database, test a Request/Response, need the Container etc.; you need framework features that are bootstrapped using the CreatesApplication trait which is included with the Laravel testCase Class.

It mioght be easiest (to begin) to create Feature tests; you make a Request in the test example and assert against the Response and/or side-effects (like creating database records, sending mails etc.). Whenever necessary, you can drop down to Unit test a specific piece of functionality.

Tray2's avatar
Tray2
Best Answer
Level 73

There are quite a few really good course on TDD in Laravel. I recommend checking out the Spatie and Chrisoph Rumpel courses

https://learn.christoph-rumpel.com/products/pest-driven-laravel

https://spatie.be/products/testing-laravel

There is an older course by Adam Wathan that is really good as well.

https://course.testdrivenlaravel.com/

And of course there are some of them here at Laracasts as well.

https://laracasts.com/topics/phpunit

https://laracasts.com/topics/pest

cwhite's avatar

Pest has the following default:

uses(TestCase::class)->in('Feature');

which means it uses the Laravel TestCase class for all tests in the Feature directory (the Unit directory uses the PHPUnit TestCase class). You could always update this to whatever you want, but I like the distinction that if a test needs the Laravel TestCase, then it's a feature test.

Please or to participate in this conversation.