Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Match test directory to app directory

I have a pretty big Laravel 5.6 application I'm currently developing. By default, Larvel comes with a tests directory along with a tests/Unit and tests/Feature sub directories.

  • When I first started developing my app, any test I wrote that didn't require a full HTTP life cycle to take place, I would put in the tests/Unit directory.

    • Example: Testing a custom local query scope on a model.
  • Similarly, any test that did require a full HTTP life cycle I would put in the tests/Feature directory.

    • Example: Testing controllers using a GET, POST, etc request and making sure I got back a 200 response or equivalent.

Now that my application has grown to be quite massive, I'm starting to think that there should be a better way to organize my tests directory. I'm also noticing that parts of my code base do not have test coverage based on reports from codecov.io.

In my mind, it would make sense to create a tests/App directory and have that mirror all my files and classes in the app directory of the project root. Each class and/or file would have a corresponding test. This way, I'll have a better idea of which parts of my app have test coverage and which ones do not. I haven't seen any other projects that do this so I'm hesitant to apply this approach.

So, to summarize my question:

Are there any pitfalls with structuring the tests directory exactly like the app directory?

Is there an alternative way to structure your test directory so that you can tell with ease which classes/files have corresponding tests?

Side Note: I'm well aware that my unit tests don't adhere to the "normal" convention of mocking out your dependencies and testing in isolation. This is a separate issue but in short I feel that mocking doesn't allow you to refactor you code with ease. More info on what I'm talking about in my defense: https://www.youtube.com/watch?v=LdUKfbG713M

like image 787
Denis Priebe Avatar asked Dec 07 '25 05:12

Denis Priebe


1 Answers

I'm also using the structure that Laravel provided. but I have made little changes. for example for functional testing, I have added an admin folder so I can write tests for admin APIs or for unit testing I have added (unit/cron, unit/middleware, unit/jobs, ...) this way I can control the directories.

tests/functional/admin

tests/unit/cron

tests/unit/middleware

tests/unit/jobs

tests/unit/policy

tests/unit/models ...

And also to see which API or function has a test or not it's a little hard to have it. If you are following the TDD principle then no need to worry about this part because you know that everyone who writes a piece of code he/she will write the test for it. but if you are not following the TDD and you have just started with writing tests for your project then you need to run PHPUnit coverage with --HTML tag and it'll return for you the HTML graph which can tell you how much code coverage your project has.

like image 153
Mohammad.Kaab Avatar answered Dec 08 '25 21:12

Mohammad.Kaab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!