自动对文件更改运行特定的测试? [英] Automatically run specific tests on file change?

查看:47
本文介绍了自动对文件更改运行特定的测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to automatically run specific tests when specific files are changed, similar to what you can do with a Guardfile in Ruby on Rails. I was wondering if there is a way to do this with Laravel Elixir or with gulp (I.e. gulpfile.js)

以下是我正在寻找的示例:

Here is an example of what I'm looking for:

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源文件执行此操作,并且可以正常工作.

You can do this with grunt and a couple of the plugins, if that is an option for you. I do this for PHP, javascript and CSS source files and it works a treat.

示例gruntfile,已修剪:

Example gruntfile, trimmed down:

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 现在,只要提供的中的src/中的php文件发生更改,此操作就会在phpunit中运行,并且grunt watch任务将在后台运行.您当然可以在监视任务的文件"部分中使用正则表达式模式来限制和更改要监听的文件.

This will now run phpunit any time a php file within src/ changes, provided you have the grunt watch task running in the background. You can of course restrict and change which files you listen for with regex patterns within files section of the watch task.

要基于特定文件更改而不是通配符update-all运行特定测试,您将具有以下条件:

To run specific tests based on a specific file change rather than a wildcard-update-all, you would have the following:

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

如果您的用户测试跨越多个目录,则使用测试套件是更好的使用方法.因此,如果您想运行测试/用户和测试/Auth等中的所有测试文件,则在phpunit.xml文件中会有一个测试套件来运行这些文件.像这样:

Using test suites is the better route to use if your User tests span multiple directories. So if you want to run all test files within tests/Users and tests/Auth etc, you'd have a testsuite in your phpunit.xml file that would run those. Something like:

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

这篇关于自动对文件更改运行特定的测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆