Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run unit test when saving file in PhpStorm

I have set up PhpStorm 5 with PHPUnit, and I'm curious if PhpStorm might have some functionality that will automatically run a unit test when saving a file. Like watchr and guard. I have tried search our beloved www and the PhpStorm docs, but haven't been able to find a solution for it.

like image 761
Ole Aass Avatar asked Jan 13 '13 09:01

Ole Aass


People also ask

How do I run a single test case in intelliJ?

Rerun a single testRight-click a test on the Test Runner tab of the Run tool window and select Run 'test name'.

How do I run a JUnit test in intelliJ?

In eclipse, you go to test class-> right click -> run-> as junit. Somewhat like this we do. Its similar in intellij too. Go to test class right click and run as junits.


1 Answers

As of version 6, PHPStorm has "File Watchers"

  • Open your project preferences.
  • Select File Watchers from the left hand list of options.
  • Click the + symbol at the bottom of the empty right hand panel.
  • Select <custom>

You will have to set up a command line for PHPUnit, it wont be the integrated testing, but you can have errors output to the console (which is good a good start!)

Various macro options are available to you, so you can include (for example) $FileNameWithoutExtension$Test.php in the arguments passed to your command line.

I personally had to set up two watchers. The first detected modifications to project files, and the second detected changes to test files (the second did not append Test.php to the filename) I also created a new project scope to exclude the tests directories from the first watcher.

You may also want to turn off immediate synchronisation, as this causes PHPUnit to run when PHPStorm auto-saves files.

My other settings are like:

  • File Type: PHP files (PHP)
  • Scope: Project excluding tests
  • Program: /path/to/php
  • Arguments: /path/to/phpunit --configuration /path/to/phpunit.xml.dist /path/to/tests/$FileNameWithoutExtension$Test.php
  • Working directory: $FileDir$
  • Output paths: $FileDir$

No output filters set, syntax error checks enabled, and console showing errors.

like image 191
Leigh Avatar answered Oct 26 '22 19:10

Leigh