Running PHPUnit tests in PHPStorm
Did you know that PHPStorm has a nice intergration with PHPUnit? Running tests directly from IDE offers several benefits that can boost your productivity to some extent:
- Running a test method / test class / test suite by simply pressing a combo on your keyboard
- Same as point 1 but I have to mention it separately: you can do it on a remote server as well — via SSH
- Failures are reported directly in the IDE, and a click on a file name takes you directly to the required line.
To have your tests run in PHPStorm, you will have to set up a few things:
- PHP interpreter
Setting up local PHP interpreter
Go to Settings ▶ Languages & Frameworks ▶ PHP
It’s pretty much self-explaining, as you can see, and you are likely to have this already set up, if you actively do PHP development.
A lot of companies use remote sandboxes to run development version of the project, run tests etc. For that, you can setup remote interpreter by clicking on the ellipsis button next to CLI Interpreter dropdown:
Setting up PHPUnit
Go to Settings ▶ Languages & Frameworks ▶ PHP ▶ PHPUnit
You can select a PHP interpreter, path to PHPUnit executable (often it is the path to phpunit.phar but I prefer to symlink it to have a shorter name). If you use XML configuration and/or a bootstrap file, those can be specified for your project as well.
Easy as 2x2. Select the whole folder with tests in the project tree and press a combo (Ctrl+Shift+F10 in my configuration which has Eclipse keymap as a base; find yours in Settings ▶ Keymap). Alternatively, right-click folder name and choose “Run PHPUnit”.
But that’s not all PHPStorm can do for you. Place the cursor inside a test method and press the same combo: PHPUnit will run just that particular test method. Extremely handy when actively developing something and constantly switching between working code and test code and running the test. Moreover, if you place the cursor outside a test method, while still staying within the test class and run PHPUnit — the whole test class is run.
Keep calm and write unit tests. See you later ;-)