简体   繁体   English

在更改时运行PHPUnit测试

[英]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. 每当文件在磁盘上发生变化时,我都想运行我的PHPUnit测试(或者至少是它们的一部分)。 Very similar to what you can do with " grunt watch ". 非常类似于“ grunt watch ”所能做的。 I have a project in which I have both JS and PHP, and am using Grunt. 我有一个项目,我有JS和PHP,并使用Grunt。 There I shell out to PHPUnit to have it run on top of my JS tests using grunt watch. 在那里我向PHPUnit发出请求,让它在我使用grunt watch的JS测试之上运行。 While that works just fine, it seems like an awful lot of hassle to do this in a PHP only project. 虽然这样做很好,但在仅PHP项目中执行此操作似乎非常麻烦。 I'd need to introduce grunt, and add a dependency on node. 我需要引入grunt,并在节点上添加依赖项。 Plus I have a lot of such PHP projects. 另外,我有很多这样的PHP项目。 A more simple solution is thus in order. 因此,更简单的解决方案是有序的。

You can use node watch which is quite sweet: 你可以使用很甜的节点watch

Run npm init to set up a package (takes a minute), then 运行npm init来设置一个包(需要一分钟),然后

npm install --save-dev watch

Then edit your package.json file to include these two entries: 然后编辑package.json文件以包含以下两个条目:

"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/ 我前段时间写了一篇博文: 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 ". 这意味着“监视此目录中的所有文件以及下面的更改,如果其中一个更改运行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 它仅监视.php文件的更改

A dead simple way to do this, using inotifywait and a bash script: 使用inotifywait和bash脚本执行此操作的简单方法:

#!/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. 我正在运行tmux,所以我只是设置了一个堆叠的窗格,有一个小窗口可以查看我的测试结果。 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). 此外,此脚本是Vim友好的(Vim创建一个文件,4913,在保存期间,此脚本忽略它)。

It was totally inspired from various places around the web -- probably even from some of these answers. 它完全受到网络各个地方的启发 - 甚至可能来自其中一些答案。

Maybe you can try phpunit-watcher . 也许你可以尝试phpunit-watcher https://packagist.org/packages/spatie/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: 首先,安装带有composer require spatie/phpunit-watcher --dev phpunit-watcher composer require spatie/phpunit-watcher --dev ,然后修改composer.json以在scripts属性中添加一个项:

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

Then, you can run test cases with: composer test . 然后,您可以使用: composer test运行composer test

Sometimes, you should run composer run test --timeout=0 , otherwise the process would be killed after 300 seconds. 有时,你应该运行composer run test --timeout=0 ,否则进程将在300秒后被杀死。

Install grunt and use https://github.com/SaschaGalley/grunt-phpunit 安装grunt并使用https://github.com/SaschaGalley/grunt-phpunit

Then you'd setup the phpunit task under with the watch task. 然后你用watch任务设置phpunit任务。 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. 在PHPStorm中,您可以配置文件监视器,它将在文件修改时为您执行shell命令。 For example you can have one File Watcher that will trigger on php file modification and will run phpunit command with all necessary commands. 例如,您可以拥有一个将在php文件修改时触发的文件监视器,并将使用所有必需的命令运行phpunit命令。

More info about File Watchers can be found on JetBrains web help page: https://www.jetbrains.com/phpstorm/webhelp/using-file-watchers.html 有关File Watchers的更多信息,请访问JetBrains网页帮助页面: 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 您可以通过运行composer require --dev icyapril/whats-changed来通过composer将其拉入

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. 有了这个,只需运行./vendor/bin/whatschanged ,只运行上次提交或工作目录中更改的文件。 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. 我找到了phpunit-testrunner节点包,这对我的问题有用。 One config json file per PHP project, which can then be used by anyone that has the node package installed. 每个PHP项目一个配置json文件,然后可以由安装了节点包的任何人使用。 Also cross platform. 也跨平台。 Still requires node though, so not an ideal solution. 仍然需要节点,因此不是理想的解决方案。

i found this solution use node+gulp 我发现这个解决方案使用node + gulp

0. install node 
1. npm init 
2. npm install gulp --save-dev

create a gulpfile.js and put this 创建一个gulpfile.js然后放这个

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 my_project_name / App是所有源代码的路径

if add extension add this line on default task 如果添加扩展名,则在默认任务中添加此行

gulp.watch('my_project_name/ /*.otherext ', ['phpunit']); gulp.watch( 'my_project_name / /*.otherext',[ 'PHPUnit的']);

after edit php file run the phpunit test 编辑php文件后运行phpunit测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM