02. Pest up and running


Okay, so the goal for this part is to get a fresh Laravel project set up so we can run tests with PEST and that includes our database as well. We're not going to actually write a test that hits the database
but we'll get everything set up so we can do. Okay, so the first thing that we're going to do is just create out a fresh project. Let's go ahead and call this book friends like we know and we'll just wait for that to finish.
And now that's done let's go ahead and open this up in our editor and there we go. Okay, so the first thing that we're going to do is just head over to env, the pretty standard stuff. Go ahead and switch over the database connection details.
So I've already got a database called book friends. I'm using Postgres here. I'm just going to switch the port over and the username for me just running locally is my full name.
Okay, so we're going to go and run phpartisan serve for this. Doesn't really matter which environment you're using to serve your project but we're going to go ahead and just run that, much easier. And let's go ahead and run phpartisan migrate.
Like I said I've already got this database set up here so just running that is going to run all of the default migrations. Pretty standard stuff if you're used to working with Laravel. Okay, so now that we've done that let's actually just run
phpunit as it is with Laravel and see what happens. Okay, so we've got two out of two tests have run and both are successful. So if you are new to testing in Laravel, this tests directory contains a feature and a unit folder
both of which have an example test. So you can see here this is class-based. Once we get into pest you'll notice that it's not and all we're doing here is hitting the home page
and checking that we get a 200 response code. Pretty standard. And for our unit test if we just scroll down here we're just asserting that the value true is true.
So pretty standard. So we're actually going to go ahead and delete these because we're not going to need these. So let's get rid of these ones now.
And now we're going to go ahead and install pest. So if we come over to the documentation the first thing we need to do is requiring the base pest package. So let's go ahead and pull that in first of all.
And that's just about done. And because we are working with Laravel we want to go ahead and pull in the Laravel plugin. This is going to allow us to use Laravel specific functionality within pest.
So we're going to go over and paste this in. And once that's finished we're going to run phpartisan pest install which is going to do a couple of things. So let's go over here and check out this new pest.php file
that's been created in our tests directory. So if we come down here the first thing that you'll see is this uses function. Now the test case is this test case just here. So we're still making use of the Laravel functionality here with this test case.
But this is going ahead and just providing a shortcut to where it's being used. So this is being used in feature tests. If you also wanted this to be used in unit tests for example if you wanted to use database migrations in your unit test
you can go ahead and add that in there. We're not going to be doing that here but that's an option. Now if we scroll down you can see that we've got a custom expectation here. So we can actually define a custom expectation within pest.
We'll get to that a little bit later and we can also define custom functions as well which we will also get to a little bit later. But what we want to figure out now is how are we going to run our tests. Well that's really simple and we saw that from the introduction.
We're just going to run pest on its own and you can see here no tests executed. So we want to generate a test and just create something out similar to what we got with the base Laravel installation. So let's go ahead and run phpartisan make test.
This is how we would normally make a test. And let's go ahead and just create an example test again and let's use the unit flag to generate our unit test. Now running this on its own if we go ahead and run this
is going to give us pretty much what we saw when we first installed Laravel. We've still got this class example test and we've still got this function or this method of this class just here. That's not what we want.
We want a pest specific test with the pest syntax. Now how do we do that? Well Laravel also comes with the ability to run the pest flag. This isn't specific to pest when we create this.
This is a Laravel feature. And when we go ahead and run that with a pest flag that's going to create pretty much the same test but with pest functionality.
And that is how simple it is. There's no class here. It's just a test function and the name of the test and then the expectation. So we can go ahead and run pest now and that's going to run that test.
Obviously pass because true is true. And that's pretty much it. So we've got pest up and running and we've got this example test here. Let's just change this around and look at the sort of structure of a pest test.
So this uses this test function and we can say it is true. So test it is true. Expect true. So this is a pest function.
We pass a value in here and then we use any of the available methods on pest. So for example we could say test is false. And the way that we would write that is say expect false and to be false. To be false.
Or we could say to be false. Or we could say to equal false. Or if we wanted to say something completely different we could say not to be true. So there are a huge amount of assertions that we can create.
We're not going to cover all of them because they're all in the documentation. But as you can see when we run this we go ahead and get a pass. So all of this is over in the documentation. The last thing that we're going to just talk about is this test function.
This is a complete personal preference but what we can also do is use the it function. So I could say it is false. I much prefer this way in terms of reading it. But if you prefer the test function go ahead and use that.
So it is false. Again that's just going to work in exactly the same way. Now we can also generate out feature tests as well. So let's just take a really quick look at that before we move on.
So I'm going to go ahead and say phpArtisan make test. I'm not going to pass in the unit flag. I'm going to say example test but I am going to pass in the pest flag. That's going to generate our feature test for us.
Again much like what we saw when we first installed Laravel. But this time we have pest specific syntax. So I can say it shows the home page or something similar to that. And we can go ahead and again run pest and you can see that this works.
Now a little bit later on we are going to be working with our database. So it's really important to set this up now so we don't hit our actual production database or our development database. So for the purposes of this we can just use something like sqlite.
That kind of makes sense. We have an in-memory database. We don't have to create a separate database. So we're going to go over to phpunit.xml which you'll find as standard in any Laravel project.
Anyway you can see that you've got the unit test directory, the feature directory here, any of the files that you want to target. And down here what we can do is go ahead and uncomment sqlite and uncomment memory. So they're the two key things that we need to do before we start.
Because once we do start hitting the database and using factories we want to create these in an in-memory sqlite database. Okay so that's pretty much it for getting started with pest. Let's head over and actually start to build out our app so we can write some real tests.
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.