r/PHPhelp • u/Haunting_Barnacle_63 • Oct 08 '25
Test Duration
How long does it take to run all your tests?
Even with parallel test execution our testsuite takes ~5 minutes.
Reason is probably that too many test rely on DB and tests are constantly writing and reading from the DB, which seems totally common in Laravel applications.
4
Upvotes
3
u/andrewsnell Oct 08 '25
For what size and kind of application? I've got standalone API microservices that run ~1,000 tests in less than a second, including tests that touch the persistence layer. I've also got a legacy project that runs ~47,000 "pure" unit tests in parallel in 30 seconds, but takes about 8 minutes to run ~8,000 tests that boot the full application and manipulate the database. There's some art to having the right number of the right kind of tests.
2
u/obstreperous_troll Oct 08 '25
You may be able to switch many of your tests to use a sqlite in-memory database instead, depends on how many db-specific features you're using (I have one app where this works fine, and another using postgres features that makes it impossible). But yeah, Laravel's idea of a "Unit" test is just risible. I suggest making as much business logic as possible use DTOs and not Model classes, which will make it unit-testable for real.
2
u/mr-slappy Oct 08 '25
You want to look into the testing pyramid. If you have that many tests connecting to your DB then that's a big code smell.
Either from a testing strategy POV, or so much of your code is that tightly coupled that it can't be tested in isolation. In which case you've probably got bigger problems with your codebase.
4
u/MateusAzevedo Oct 08 '25
Not probably, this is the cause.
And that is because of code written The Laravel Wayâ˘. Really, everything you see in the documentation, tutorials and articles online just make unit testing harder.
The proper solution? Better following the test pyramid, but that also means writing code for unit tests.
1
u/Timely-Tale4769 Oct 09 '25
Is there any other test type available in PHP other than unit test?
2
u/rmb32 Oct 08 '25
Maybe too late in the game regarding your codebase for my suggestion⌠But the repository pattern helps with this stuff.
Write a repository interface that accepts POPO (Plain Old PHP Object) entities. Implement your production repository to convert them to Eloquent models to persist/retrieve. Then write another âdummyâ implementation for testing where the given POPO entities are assumed to succeed or fail.
Switch which of the repository implementations the container should inject to your services depending on environment (âtestingâ vs âproductionâ).
This avoids the database when testing. However, for true âend-to-end testsâ, youâll want a real database which is ephemeral.