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.
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 )
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
A dead simple way to do this, using inotifywait and a bash script:
#!/bin/bash
while true; do
FILE=$(inotifywait --exclude=".*.*sw*" --exclude="4913" ./ --format "%w%f" -e close_write) &&
clear &&
phpunit --color $FILE
done
Then you just run that in a terminal (or however you want) and you're good. I'm running tmux, so I just have a stacked pane set up, with a little window to see my test results. Just run this script in the directory where your tests live.
Works well for me.
Also, this script is Vim friendly (Vim creates a file, 4913, during saves, this script ignores it).
It was totally inspired from various places around the web -- probably even from some of these answers.
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.
Install grunt and use https://github.com/SaschaGalley/grunt-phpunit
Then you'd setup the phpunit task under with the watch task. This is how I do it right now.
In PHPStorm you can configure File Watcher that will execute shell commands for you at file modifications. For example you can have one File Watcher that will trigger on php file modification and will run phpunit command with all necessary commands.
More info about File Watchers can be found on JetBrains web help page: https://www.jetbrains.com/phpstorm/webhelp/using-file-watchers.html
I've created a solution to deal with this very problem: " What's Changed? " In short, there are cases where you have some really large test suites and you only want to run the tests relevant to the classes which have changed.
You can pull this in via composer by running composer require --dev icyapril/whats-changed
With this in place just run ./vendor/bin/whatschanged
and only the file changed in your last commit or in your working directory will be run. Magic!
您可以使用inotifywait
,有关PHPUnit示例,请参阅https://speakerdeck.com/rowan_m/building-better-developers-2?slide=31 。
I found the phpunit-testrunner node package, which is of use for my problem. One config json file per PHP project, which can then be used by anyone that has the node package installed. Also cross platform. Still requires node though, so not an ideal solution.
i found this solution use node+gulp
0. install node
1. npm init
2. npm install gulp --save-dev
create a gulpfile.js and put this
var gulp = require('gulp'),
util = require('util'),
exec = require('child_process').exec;
gulp.task('phpunit', function() {
exec('phpunit -c my_project_name/App', function(error, stdout) {
util.puts(stdout);
});
});
gulp.task('default',['phpunit'], function() {
gulp.watch('my_project_name/**/*.php', ['phpunit']);
});
my_project_name/App is the path of all source code
if add extension add this line on default task
gulp.watch('my_project_name/ /*.otherext ', ['phpunit']);
after edit php file run the phpunit test
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.