PHPStorm: Auto Run PHPUnit On File Save

November 19 2015, 3:11pm

You can setup PHPStorm to run PHPUnit every time you save a unit test. By doing this you get immediate feedback if your unit tests are passing as you write them. And by running PHPUnit only against the current file you’re editing it can run really fast.

To set this up we need to create a File Watcher:

Preferences > File Watchers

Click (+) sign and select custom to open the new file watcher dialog. Add the following settings:

  • Name: PHPUnit
  • File type: PHP
  • Scope: click the to create a new one called tests and use a pattern like file:tests/*.* if tests is the folder all your unit tests are located in and call it tests or something.
  • Program: $PhpExecutable$
  • Arguments: vendor/bin/phpunit --configuration phpunit.xml.dist $FileDirRelativeToProjectRoot$/$FileName$
  • Working directory: $ProjectFileDir$