Lessons: 34Length: 2.5 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.3 Easier Testing With Snippets

One thing that would be nice is if Sublime Text could give me a easy way to prepare my tests. So in this case I'm using php, so I'm using php unit. And I frequently have to do things like this assert equals. But then from time to time you forget, is this supposed to be what's expected, is this what we're matching against? As you may know these things can slightly be different from method to method. So if we tried to install a solution. We'll look for phpunit. All we have right here is a plugin, but that doesn't really have the snippets that I'm interested in. But don't always depend on Package Control. Sometimes you'll find resources that simply haven't been added to Package Control. So in these cases, GitHub can be an excellent resource. So I've done a search for phpunit snippets, and I came up with this repo. And take a look. There's probably around 50 different snippets. So let's go ahead and bring this into our project. I'm going to copy the URL here, and then, within the terminal, I'm going to CD into my library, on the MAC into Application Support, Sublime Text 2, and into the Packages directory. Within here, we need to switch into the User directory. So CD into the User directory. Now assuming that you have git installed, I can run git clone I paste in the url and we're gonna call it PHPUnit. And that's it. We now have that PHPUnit directory. If I list files, there is all of our snippets. So now I have free reign over all of the snippets. If I run ass. Now you'll start to see all of the snippets that are available to us. So in addition to providing a shorthand, it's also a great reference when I'm not exactly sure which method I need? Well I can quickly search through this list here. Maybe I need assertContains and then that will expand. Here is the needle, my string. We're going to look into haystack of my string. Hit Tab again. If I want a message, I can add it. Otherwise I can hit Backspace, and now I'm done. So maybe I need to assert by regular expression. I could do ass for assert, r and there we go, assertRegExp. The only other thing that I see myself needing is the ability to quickly write these test functions. So why don't we create a snippet ourselves? A new snippet, and right here, I will do public function test${}(), and then I will set the caret right after that. And then, we'll set the next stop point right there. All right? I'm gonna set the scope to source.php, and the tabTrigger is going to be test. I'm going to save this into my PHPUnit directory, and we'll call it test.sublime-snippet. Again, remember, it has to have that extension in order to work. So once that has been saved, let's try it again. We'll delete everything entirely. And now we're going to write a test, test Tab. The name will be CanCreateUser, something like that. And now we'll assert equals, and everything is working as expected. So I love this, because it turns the process of writing your test into only a few keystrokes.

Back to the top