Run PHPUnit tests from VSCode

 Visual studio marketplace version badge Visual studio marketplace installs badge Visual studio marketplace rating badge

At Travel to Live, we love the open source community and we have a bunch of it in our daily arsenal. So, we thought, time to give something back, even if just a small piece! I’m sure anyone reading this is familiar with the famous text editors Sublime text, notepad++ and Atom. But have you tried the new (out of beta under a year ago) Visual Studio Code? Yes, I know what you’re thinking. Visual Studio? Microsoft? Are they making an open source text editor? The answer is yes! And it even runs on Linux and Mac! Yea you heard that correctly. Microsoft is stepping up their open source presence a great deal. Now, what better way to give back to the community than creating a PHPUnit extension in the text editor I use daily, yea you guessed it, Visual Studio Code.

Make everyday work easier

Our back-end is built with PHP, and we do our testing with PHPUnit. However running tests from the command line is no exciting task. You have to start up the command prompt and type the right commands and filter so that it only tests the function you want and you have to do it over again when you want to test another function. But what if you want to test a whole class, what’s the command for that again? As coders, we are lazy and just want to be able to do the thing we want by some command and only solve the problem once. And this is where I saw a chance to fill a pot hole.

The PHPUnit Extension

The extension provides a simple way to run single or multiple PHPUnit tests with minimal effort. Just like Sublime and Atom, VSCode has a command palette where you can execute commands. And one command can be used in different ways depending on the context. So, for example, if your cursor is currently on a function and you run the command, it will test that function. But if you put the cursor on the class, it will run all tests in that class instead. Dead simple. Have a look at the GIFs below.

My Visual Studio Code extension for PHPUnit runs any test function quickly and easily!

Testing a function

My Visual Studio Code extension for PHPUnit tests a whole class quickly and easily!

Testing a class

VSCode also provides a really simple way to pass configurations to your extension, so all the user has to do is open his settings and enter the path to PHPUnit and a string with additional arguments (that can be anything PHPUnit accepts). Settings in VSCode are all in JSON, no tucked away settings in some sub-menu somewhere; it’s all there, searchable in a JSON file.

{
    "phpunit.execPath": "path/to/phpunit",
    "phpunit.args": [
        "--configuration", "./phpunit.xml.dist"
    ]
}

My experience writing the extension

Extensions are written in TypeScript, which is basically ES6 with types, so no need to learn any complicated things there. VSCode also provides autocomplete into its API, which makes it really easy to find the functionality you’re looking for. Getting started couldn’t be easier. Their documentation explains everything step by step so you can have a “Hello World” extension up and running in no time. All and all, it took me about a weekend to go from not knowing anything about how to make extensions in VSCode to having published it. Writing the actual code was really quick: not a lot of code was required for this extension. If you’re interested in making an extension yourself, know that with VSCode it’s quite simple! Now, if you’re already using VSCode and PHPUnit go ahead and check out my extension!