自动 运行 对文件更改进行特定测试?

Automatically run specific tests on file change?

我正在寻找一种在特定文件更改时自动 运行 特定测试的方法,类似于您可以在 [=16] 中使用 Guardfile 执行的操作=]Ruby Rails。我想知道是否有办法用 Laravel Elixir 或 gulp (即 gulpfile.js)

这是我正在寻找的示例:

watch('^app/Http/Controllers/(.+)(Controller)\.php$', function($match) { 
    return ["tests/{$match[1]}"];
});

watch('^app/Policies/(.+)(Policy)\.php$', function($match) { 
    return ['tests/' . str_plural($match[1])];
});

watch('^app/User.php$', function($match) { 
    return [
        'tests/Admin',
        'tests/Auth',
        'tests/Users',
    ];
});

您可以使用 grunt 和一些插件来完成此操作,如果您愿意的话。我为 PHP、javascript 和 CSS 源文件执行此操作,效果很好。

示例 g运行t 文件,已缩减:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        watch: {
            grunt: { files: ['Gruntfile.js'] },
            php: {
                files: ['src/**/*.php'],
                tasks: ['phpunit']
            }
        },
        shell: {
            phpunit: 'phpunit --testsuite Unit' // or whatever
        }
    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-shell');

    grunt.registerTask('phpunit', ['shell:phpunit']);
};

你需要grunt-contrib-watch and grunt-shell

现在 运行 phpunit 任何时候 src/ 中的 php 文件发生变化,提供 你有 grunt watch 任务 运行ning 在后台。您当然可以在监视任务的文件部分使用正则表达式模式限制和更改您监听的文件。

编辑:

要运行 基于特定文件更改而不是通配符更新所有的特定测试,您将具有以下内容:

watch: {
    grunt: { files: ['Gruntfile.js'] },
        UserSrc: {
            files: ['app/**/UserController.php'], // The ** matches any no. of subdirs
            tasks: ['UserControllerTests']
        }
    },
    shell: {
        userTests: 'phpunit tests/User' // To run all tests within a directory, or:
        //userTests: 'phpunit --testsuite UserController // to run by testsuite
    }
}
// ... Other config

grunt.registerTask('UserControllerTests', ['shell:userTests']);
// ... more tasks

如果您的用户测试跨越多个目录,则使用测试套件是更好的选择。因此,如果您想要 运行 tests/Users 和 tests/Auth 等中的所有测试文件,您的 phpunit.xml 文件中会有一个测试套件可以 运行 那些。类似于:

// ...
<testsuite name="UserController">
    <directory suffix="Test.php">tests/Users</directory>
    <directory suffix="Test.php">tests/Auth</directory>
    // .. Other directories
</testsuite>
// ...