Search code examples
phplaraveltestingphpunit

Testing strategies for a Laravel web api


I am a beginner with both laravel and software testing in general. My question here is not how to use the PHPunit or whatever testing framework's assertions or provided helpers. My main concern is to have some insight on how you lay out the testing approach to an MVC web API on a framework like laravel. Like what you should put in mind while testing and wether you should use unit or integration testing, what do you do with models, controllers, policies, middlewares, etc. So if there are any experienced testers with laravel out there I would be grateful to have some insight.


Solution

  • @ramy-farid I think this question is overly broad for StackOverflow. I hope the following resources can help:

    If you're looking for a starting point consider checking out the testing series available on Laracasts: https://laracasts.com/skills/testing if you're not subscribed then I highly recommend it!

    Also, consider reading through the docs specific to testing to see what testing features you have within your grasp out of the box https://laravel.com/docs/5.3/testing

    Adam Wathan has some great resources on testing which are available on his website and some of his talks at events and podcasts (including his own podcast):

    Adam's website: https://adamwathan.me/

    Talk from Laracon: https://streamacon.com/video/laracon-us/adam-wathan-test-driven-laravel

    Adam's podcast: http://www.fullstackradio.com/ (search for the word test on the page I'm sure you'll find plenty of talks)

    Adam also has an up and coming video series on building an application with Test Driven Development later this month (28 Nov 2016 - writing the date from my memory) https://adamwathan.me/test-driven-laravel/ - this will be a valuable resource as he'll be building an app from scratch and testing all the way

    You may also want to check out the Laravel Podcasts for testing topics here: http://www.laravelpodcast.com/episodes

    Jeffrey Way also writing a book on testing (not released yet), which will be free, see link here: https://leanpub.com/testingphp

    If that's a bit much, consider just watching Adam's video as I think he lays out a nice approach for testing "The outside-in TDD loop" which he begins explaining at about 8mins in to the video https://streamacon.com/video/laracon-us/adam-wathan-test-driven-laravel

    To address part of your question:

    wether you should use unit or integration testing

    For me, it shouldn't be a choice of integration or unit testing, why not use both if that helps achieve the purpose of writing your tests which I assume is something along the lines of making sure your app works the way it's supposed to.

    Hope this helps!