Run PHPUnit tests on change

后端 未结 10 1705
星月不相逢
星月不相逢 2021-02-07 11:34

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

相关标签:
10条回答
  • 2021-02-07 11:51

    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.

    0 讨论(0)
  • 2021-02-07 11:53

    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)

    0 讨论(0)
  • 2021-02-07 11:53

    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.

    0 讨论(0)
  • 2021-02-07 11:57

    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

    0 讨论(0)
  • 2021-02-07 11:58

    You can use inotifywait, see https://speakerdeck.com/rowan_m/building-better-developers-2?slide=31 for a PHPUnit example.

    0 讨论(0)
  • 2021-02-07 11:59

    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

    0 讨论(0)
提交回复
热议问题