Testing code is one of the more challenging parts of programming, but luckily Laravel and some other tools are making it easier than ever. Research has even shown implementing a Test-Driven Development (TDD) approach can significantly reduce the number of bugs that make it to production. Testing provides many other benefits, like the freedom to refactor large parts of a system without (as much) fear of breaking things.
Everybody has a testing environment. Some people are lucky enough enough to have a totally separate environment to run production in.
— Papa Devops (@stahnma) August 21, 2015
With all of the benefits of testing, it’s still challenging to continually test applications. If you are looking to start learning how to test Laravel applications or you want to expand your skills here is a list of great resources to help you:
The Official Documentation
The official Laravel documentation is arguably the most complete testing guide that ships with a PHP framework. Not only does the guide itself make it very easy to start testing Laravel applications, but the framework itself also provides essential tools that take away the excuses of not testing. The most important features to make testing easier—that ship with Laravel—include database migrations for testing, factories, and mocking/fakes.
This first resource is obvious, but those new to Laravel and testing applications, stop by the documentation first. As a companion to the documentation, watch the Laracasts video Laravel From Scratch: Testing 101 for an overview of testing Laravel.
Laravel Dusk is a browser testing package that works with
>= Laravel 5.4. The Dusk documentation is part of the official Laravel documentation, but it’s worth mentioning separately because it is a recent addition to the Laravel testing tools. Dusk doesn’t require Selenium but uses the standalone ChromeDriver—which ships with the package.
The Orchestral Testbench Component
The Orchestral Testbench package is a testing helper for Laravel package development. According to the readme file, the Orchestral Testbench package:
…is a simple package that has been designed to help you write tests for your Laravel package, especially when there is routing involved.
The Laracasts Laravel Behat Extension
Behat is the defacto tool for Behavior-Driven Development (BDD) in the PHP community, and this package can help you get started with Behat and Laravel quickly. Before Laravel Dusk was available, full browser testing automation was only possible with third party packages and tools.
Although Dusk is now available, Behat might be a tool you prefer for acceptance testing if you prefer the BDD flavor of testing with Cucumber. Laracasts provides a lesson on using Behat,Laravel 5 and Behat: BFFs, which will help you get started with Behat 3 and Laravel 5.
phpspec is a tool for writing BDD Specifications in PHP. The interesting thing about phpspec is how it guides you through small steps in writing test specifications, then making them pass. Learning phpspec can open your mind to a new way of testing.
You can learn how to get started with the phpspec manual. Laracasts has an introduction video to compliment the documentation. phpspec is a great tool for testing any PHP project, including Laravel packages.
phpspec Laravel Extension
phpspec Laravel Extension helps you test Laravel applications with phpspec:
This extension provides you with a bootstrapped Laravel environment when writing your phpspec tests.
Learning phpspec is a prerequisite for using this package.
Let’s Build a Forum with Laravel and TDD
The Build a Forum with Laravel and TDD series is only available to subscribers, but a the time of this writing there are 58 lessons on building a forum with Laravel and Test-Driven Development. This is one of the most complete tutorials on testing with Laravel and TDD. Even if you exhaust the generous number of free resources first, this is a great way to learn more about TDD with Laravel.
Test-Driven Laravel is a video series by Adam Wathan which provides a thorough look into testing Laravel applications. This course is not free, but as of this writing, you could still get early access. The course has many hours of courses with more still being added.
Adam Wathan initially gave a presentation on Test-Driven Laravel at ZendCon and then condensed his talk into a screencast you can watch online. We’ve also reviewed this course on Laravel News.
Spatie Laravel Packages
Spatie provides many Laravel packages, and they continue to ship new packages to the community. The packages contain a wealth of testing source code and are a good reference to help you learn how to test Laravel packages. Some packages have a dependency on the aforementioned Orchestral Testbench, providing you with some examples of using Testbench in action.
Spatie packages also have Continuous Integration with Travis CI (laravel-backup example). The murze.be blog provides Laravel tips and tricks on package development and new package announcements being released by Spatie.
The Ruby on Rails Tutorial Book
This resource is a bit of a wildcard. While not PHP or Laravel related, this free online book is chock-full of real-world testing examples you can apply to your projects. It starts picking up testing features in Chapter 3.3.
Even if you don’t care about Ruby or Rails, you will be hard-pressed to find better testing material on how to write web application tests, including the theory behind what to test and how to test it. You can follow along or just read through the material to get some great testing insight and inspiration.
Test Driven Development: By Example
Test Driven Development: By Example is considered by many a classic text on TDD. While the examples are not written in PHP, the book covers an expansive list of TDD patterns and practices and will provide beginners a good background on TDD with some practical examples.
Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.