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

时间:2016-12-22 00:44:50

标签: php laravel phpunit tdd laravel-5.3

我正在寻找一种在特定文件更改时自动运行特定测试的方法,类似于 Ruby on Rails Guardfile 所能执行的操作。我想知道是否有办法用 Laravel Elixir 或与gulp(I.e。 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',
    ];
});

1 个答案:

答案 0 :(得分:1)

您可以使用grunt和几个插件执行此操作,如果这是您的选项。我这样做是为了PHP,javascript和CSS源文件而且它很有效。

示例gruntfile,已修剪:

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-watchgrunt-shell

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

修改

要根据特定的文件更改而不是通配符更新全部运行特定的测试,您将拥有以下内容:

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>
// ...