Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run PHPUnit tests on change

I'd like to run my PHPUnit tests (or at least a subset of them) whenever a file changes on disk. Very similar to what you can do with "grunt watch". I have a project in which I have both JS and PHP, and am using Grunt. There I shell out to PHPUnit to have it run on top of my JS tests using grunt watch. While that works just fine, it seems like an awful lot of hassle to do this in a PHP only project. I'd need to introduce grunt, and add a dependency on node. Plus I have a lot of such PHP projects. A more simple solution is thus in order.

like image 945
Jeroen De Dauw Avatar asked Oct 07 '13 13:10

Jeroen De Dauw


3 Answers

You can use node watch which is quite sweet:

Run npm init to set up a package (takes a minute), then

npm install --save-dev watch

Then edit your package.json file to include these two entries:

"scripts": {
  "test": "bin/phpunit tests --color",
  "test:watch": "watch 'npm run --silent test' ."
},

Then trigger the watching with:

npm run test:watch

(credit due to this interesting article)

like image 69
ErichBSchulz Avatar answered Nov 15 '22 17:11

ErichBSchulz


I've wrote a blog post on this a while back: http://edorian.github.io/2010-03-11-running-your-unit-tests-everytime-you-save-a-file/

The most basic command would be:

pywatch phpunit .

which would mean "monitor all files in this directory and below for changes and if one of them changes run PHPUnit".

If you have a lot of files in there (assets etc) that can get slow so a better/faster version is:

find -name '*.php' | xargs pywatch "./runSuite.sh"

which only monitors changes to .php files

like image 34
edorian Avatar answered Nov 15 '22 17:11

edorian


Maybe you can try phpunit-watcher. https://packagist.org/packages/spatie/phpunit-watcher

First, to install phpunit-watcher with composer require spatie/phpunit-watcher --dev, and then, modify the composer.json to add an item in scripts property:

{
    "scripts" : {
        "test" : "./vendor/bin/phpunit-watcher watch --bootstrap vendor/autoload.php tests/"
    }
}

Then, you can run test cases with: composer test.

Sometimes, you should run composer run test --timeout=0, otherwise the process would be killed after 300 seconds.

like image 4
LCB Avatar answered Nov 15 '22 16:11

LCB