Run PHPUnit tests on change

后端 未结 10 1707
星月不相逢
星月不相逢 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 12:01

    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

    0 讨论(0)
  • 2021-02-07 12:04

    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.

    0 讨论(0)
  • 2021-02-07 12:07

    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!

    0 讨论(0)
  • 2021-02-07 12:07

    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.

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