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

benfransen's avatar

Guideline to manage the number of tests in a file

After watching the Laravel testing series here, and also a screencast from Adam Wathan I've started with a little project to give TDD a try while building a CMS. After picking features like it_renders_the_full_list_of_pages and a_user_can_create_a_page I've decided to put them in tests/features/PageModuleTest.php.

As you can imagine many tests can be created and I'm wondering if there's a rule of thumb on when to split tests into seperate files, maybe bundle them to a specific area of a PageModule in this case, for example all tests which relate to publishing/reviewing a page go into PageModule_PublishingTest.php.

How do you go along with this? Create many self explaining test-files with as little as possible tests in them or create large test classes covering, for example, a complete module and all its functionality?

0 likes
3 replies
ohffs's avatar

If your application has a lot of different modules/sections then I tend to split them up to match. But I don't think there's much of a settled opinion about it I'm afraid. For instance at the moment I'm writing a fairly focus app with 'one feature' (as far as there ever can be). So I just have a 'LogicTest' and ' UiTest' and... that's it ;-)

Other bigger applications I might split into, say, 'bookings/WhateverTest', 'bookings/SomeotherTest', 'useradmin/SomethingTest' etc. Sometimes I've also split them into 'unit/', 'integration/' etc too.

Not very helpful - sorry! I'm not very good at The One True Way ;-)

benfransen's avatar

Always helpful ;) creating subfolders is also a nice aproach. I'm just trying to "control" the chaotic situation that inevitable will occur when 100s or maybe 1000s (on larger projects) exist and you want to keep it all structured. So subfolders and SubjectMatteredTest.php seem reasonable :)

ifpingram's avatar

For "integration" level tests, I tend to group them by behaviour / feature. For example, I have the following tests for a search engine I have written:

tests/api/SaleAdvertSearch/EndToEndTest.php (2279 lines) tests/api/SaleAdvertSearch/InvalidSearchTest.php (343 lines) tests/api/SaleAdvertSearch/RetrieveAdvertByIdTest.php (68 lines) tests/api/SaleAdvertSearch/SinglePageOfResultsTest.php (150 lines) tests/api/SaleAdvertSearch/StartPageOutOfBoundsTest.php (45 lines) tests/api/SaleAdvertSearch/ZeroResultsTest.php (71 lines)

as you can see, I don't really worry too much about the size of the files. The EndToEnd test can be a little unwieldy, but I think that if I was to split it down further into all the various search permutations, I would end up with dozens of files and this itself would become a problem in the folder...

For strict "unit" tests, it is one file per class, which is standard.

Please or to participate in this conversation.