در حال بارگذاری ...

Installing BetterPHPUnit0:00

Now, as a PHP developer, finding a good workflow for using phpunit is vital to me. So yes, I can open up the integrated terminal and run phpunit here, and that's fine. However, if you want, let's see what else we could do. I'll hit Shift Command X to open up my extensions, and I'm going to look for a package called BetterPHPUnit. This is actually created by Caleb, who's active in the Laravel community. Okay, so we'll pull that in, reload, and let's try it out. So exampleTest, and now I will put the cursor either in the method name or anywhere within it. And we'll look for phpunit run.

Running Contextual Tests0:34

it. And we'll look for phpunit run. All right, so you'll see that it dynamically figures out what test should be run. In this case, it realized you were in this method, so it will trigger that one alone, and it passes. Or if we go somewhere outside of it, or on the class name, and we run it, now you'll see it only triggers that single test. So let's just do something very simple like method, and we'll say test, it loads the about page. Something like that.

Assigning Run Test Shortcut1:02

page. Something like that. All right, so let's say this, get the about page, and assert that we see about. Just very, very basic. Okay, so I can run this like so, but that's a little painful. Instead, why don't we assign this to a shortcut. So we'll close this out, open up our keyboard shortcuts, and we can see better phpunit. Okay, yeah, Command K, Command R. Generally what I do is Command T, but you can do anything you want. I do T for test.

you want. I do T for test. Okay, so now I'm going to run it. It fails, and one nice little addition is you can hit any key from within here to close it out. Okay, so let's make it pass. We'll add another route here. If you visit the about page, I'm just going to load the about page, and this should bring us to green. So yes, I could manually go back to that file and hit Command T, and we get green.

Shortcut for Previous Test1:54

us to green. So yes, I could manually go back to that file and hit Command T, and we get green. Or we have a command to run the previous test. So let's see, better phpunit, run previous. So why don't we add a shortcut for that one as well? One that I would like. So run previous, change that to Shift Command T. Okay, so now even if I'm not within the test class, Shift Command T will run it for me. So this allows me to stay in a nice workflow. The page, Shift Command T, ah, that fails.

Recap of Test Shortcuts2:21

So this allows me to stay in a nice workflow. The page, Shift Command T, ah, that fails. Let's fix it. The about page, Shift Command T, and now we're back to green, and we can close it. So are we good here? Command T within a method triggers that single method. Command T anywhere outside the method will trigger all of the tests within the file. And then Shift Command T will trigger the very last test that you ran. I use this countless times every single day, so make sure you pull it in.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟