Pest From Scratch
Pest is the new kid on the block when it comes to PHP testing frameworks. It has gained popularity quickly thanks to its modern approach, gorgeous output and focus on developer experience.
Let’s take it for a spin and see what Pest has to offer. We’ll also review some of the more advanced features that will help you to rapidly build tests for your Laravel applications.
Installation and Setup
Let’s kick things off by getting Pest installed in a project you’ve probably seen on Laracasts before: Ping CRM. Ping CRM already has a few PHPUnit tests in place, so what happens when we add Pest?
Lifecycle Hooks
Pest has no classes, so how can we set shared properties across tests. How can we execute code before or after each test in a file? That’s where Pest’s lifecycle hooks come in handy! Let’s take a look at using them in our project.
Expectations
One killer Pest feature is the Expectation API. It’s fluent, powerful and extensible. Let’s use it to add some checks to our tests and compare it with something you may already be familiar with: assertions. As a bonus, we’ll also look at adding helper functions in our Pest.php file to smooth out logging a test user into the application.
Expectations
So, you’ve seen expectations. But you ain’t seen nothing yet. Let’s revisit the same test from the last episode and add some magic. We’ll look at creating our very own expectation by extending the Expectation API. Then, I’ll introduce you to Higher Order Expectations, which can make testing Eloquent models a breeze!
Datasets
What if you want to run the same test with variations of data? Do you need to copy and paste the entire contents of the test each time? Not at all! Let’s look at datasets, a super sleek way to repeat tests and catch edge cases in your application logic.
Combined Datasets
A basic dataset will get you there 90% of the time, but occasionally you’ll need a little extra power. What if we want to combine two or more different arrays of data to create a single dataset with every possible iteration? Let’s take a look at combined datasets, which do all of the hard work for us.
Groups, Exceptions and Skipping Tests
One really cool feature of Pest PHP is supplementing a test by chaining methods onto the end of it. Let’s take a look at three such methods: group, throws and skip. We’ll discuss what problem each method solves and how using those methods fits into running your test suite as a whole.
Coverage and Parallel
As your test suite grows, you’ll want feedback on where you can improve and you’ll want to ensure your tests continue to run quickly so that you can push new features out fast. Let’s take a look at how Pest’s gorgeous coverage output helps you see where you might need to add more tests and look at how Pest’s first party parallel plugin can greatly speed up the time it takes to execute your test suite.
All That’s New in Pest v2
Pest v2 has been released, and it brings with it a host of new tools to make writing and running tests even sweeter! Let’s upgrade our demo app to use Pest v2 and take it for a spin.
Fixing –dirty Architecture
The Arch plug-in is brand new for v2. Rather than testing how your code works, it tests how your code is written. The amazing thing is that it uses the exact same syntax you’re used to for writing any other Pest test!
Intercepting Expectations
You can now override built-in expectations to test custom use-cases. That might sound confusing at first, so let’s break it down with a couple of real-world examples. It’s a power feature to be sure, but a useful one!
There are no reviews yet.