16. Pest Datasets


In the last episode, we ended up writing three tests so we could see that we have a book under each of the statuses that we included in this app. Now, this is absolutely fine. There's nothing wrong with writing tests like this.
But with PEST, we can refactor this down so we only have to write one test, but we pass slightly different data in for every single time we run this test. So let's go and just keep the first one, get rid of these two down here, and let's see what we can do with PEST data sets.
So the way that we do this is we pass an array in to this with method at the end. What this is going to do is allow us to run this test with three different or four different or however many different sets of data. Let's just examine this with three empty arrays
and just see what we get out when we run these tests in our console. So let's go ahead and just run this. And you can see it's actually run this test three times and it tells you the number at the end here.
So it shows the books that the user wants to read with and then contains an array of empty data, which of course just doesn't show anything. So what we can do is change the name of this test now to make it make a little bit more sense now that we are using a data set.
We can say something like shows books with the correct status. Because remember, the only things we're changing around are the status and the title. So what we can do now within each of these arrays, we can define this data here. So let's say status want to read.
That's the first iteration that we want to run. And the heading or title, whatever we want to call it, is going to be want to read. Now we can duplicate this down a couple of times and just change the data over. So we've got reading and the heading is going to be reading and read.
And the heading is going to be read. So this test is going to run three times. The first time with this data, second time with this data, and third time with this data. Where do we get that data from inside of here?
Well, it just comes through inside of the closure. So let's say status, heading, and now we can switch this data out. So this is the status that we're inserting when we attach that to the user. And this is the heading we want to see.
And now rather than write three different tests, we've written one test with a data set of three items. Let's go over, run our tests. And sure enough, it gives us a description of the data that we're using to put into the test.
And each of these now pass. So in future, if you have any repetitive tests like this, where you need to just pass in slightly different data, then a data set within PEST is a really good option.
35 episodes4 hrs 19 mins


Pest is a PHP testing framework that brings beautifully simple syntax to your tests, without sacrificing on features. In this course, we'll get up and running with Pest in a Laravel project and write tests for a real-world application that we'll build along the way.

You'll learn how to set Pest up in a Laravel project, write tests with Pest's built-in assertions, generate code coverage, and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.