logo
podcast Podcast
get help Get Unstuck

What is a feature test?

And how should I organize my tests?

Joel Clermont
Joel Clermont
2024-10-09

When you install a new Laravel app, it creates two directories for tests: Feature and Unit.

I've seen teams struggle with where to put their tests, and how to organize them consistently. I thought it would be useful to share how we approach this.

And to prevent this from getting too long, let's focus on feature tests today.

The Laravel testing docs introduction explains that feature tests are the preferred type of test to write, and that they exercise a larger portion of your code.

I agree on both points, but we add another layer of organization on top of that. Our feature tests are really focused on two things: HTTP requests and Artisan commands.

Every single feature test we write is testing one of those two things. Both go into the tests/Feature directory.

Within that directory we mirror the layout of the app/Http/Controllers directory, making things easy to find.

For apps with less than 10 or so commands, we'd just put those commands in the root tests/Feature directory with a CommandTest.php suffix to make it obvious what is being tested. If we had more commands, then we'd create a tests/Feature/Commands directory and mirror the app/Console/Commands directory structure within it.

I could write a whole book on all the inner workings of a feature test, but that's a project for another day. For today's purpose remember these two points:

  1. A feature test is for an HTTP request or an Artisan command.
  2. The structure within tests/Feature should follow your app/Http/Controllers or app/Console/Commands directory.

Tomorrow, I'll share some unique things we do with our unit test setup, and Friday I'll explain a third kind of test we add to every Laravel app.

Here to help,

Joel

P.S. Want me to write that testing book? Hit reply and let me know!

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.