Installing PEST in Laravel0:00
Before we can start writing our first test, we need to prepare our test setup. Since we want to use PEST as our testing framework, we need to install it. PEST is a testing framework that sits on top of PHPUnit. So even in a given project with lots of PHPUnit tests, PEST will still work. This is super useful for an easy switch to PEST, because you can just start writing new tests with PEST. Similar to Laravel, PEST is famous for a very elegant and readable syntax, and you have to write way less code when using it, but you will see all of this soon in action. Let's start by installing PEST. We're going to require it only for our dev dependencies, and then we also want to install the Laravel PEST plugin. It will give us some very nice, wonderful, unique features for a Laravel project.
and then we also want to install the Laravel PEST plugin. It will give us some very nice, wonderful, unique features for a Laravel project. When that's done, the only thing left is to install PEST. And I don't want to start a project because I have already done it, but if you haven't, make sure to give the PEST project a start on GitHub. The PEST install command will create a new PEST file under our tests directory. It's called pest.php, and it's kind of a configuration file for PEST, but we're going to take a much closer look in the upcoming videos. To make sure that PEST is really working, let's adapt this example feature test here to a PEST test. Basically, we can get rid of the whole class, so we don't need to define here a class or a namespace.
Converting First Test1:23
To make sure that PEST is really working, let's adapt this example feature test here to a PEST test. Basically, we can get rid of the whole class, so we don't need to define here a class or a namespace. The only thing we need here is a function, and I have a shortcut here to create one of those here. And let's give it a name. What we're testing is it gives back a successful response for homepage. And then I'm copying back in here the code which we had before. And now let's try to run this. And you can see our tests are passing, but mostly I don't want to run the test from the console, but I want to run them through phpStorm itself. And there's a shortcut called run context configuration. And if I run this the first time, it opens up the run configuration for the current test,
Running Tests in PhpStorm2:06
And there's a shortcut called run context configuration. And if I run this the first time, it opens up the run configuration for the current test, but it's actually only missing one thing, which is the comment line interpreter. So let's set it, and I'm using 8.1.5. I'm going to run it, and you can see my tests are also working here, and I get here some output from phpStorm itself. Also good to mention here is that if you want to run PEST tests inside of phpStorm, you're going to need the PEST plugin by the PEST team. This will make sure that also phpStorm understands the new syntax and that you can run those tests from within phpStorm.
This will make sure that also phpStorm understands the new syntax and that you can run those tests from within phpStorm. Let's close this again. Let's run this again, and I can see it's directly working. And this is pretty cool because this is how I like to run my tests because I'm super fast here. I'm just clicking in a specific test, use the shortcut, and it just gives me the output here directly. I have a video course about customizing phpStorm to get the most out of it, and this is also where I explain testing in phpStorm in depth. So if you're interested in this, please give my course, Mastering phpStorm, a look. Okay, and last, we need to check our phpUnit configuration. Since PEST sits on top of phpUnit, those configurations still work.
Configuring PHPUnit SQLite3:26
Okay, and last, we need to check our phpunit configuration. Since PEST sits on top of phpunit, those configurations still work. So we have them here in our main directory, phpunit.xml. Here we go. And here in level, in the new application, we already have those two lines which we need to comment in because for my tests, I like to use an SQLite connection, and I like to use an in-memory database. So this means we don't need a file or a real database because all of this runs in memory. This also means we don't conflict with any real database, and it's super fast. And with that being said, I think we are now already set up and good to go on and work on our Levelcast application.
and work on our levelcast application.
