【问题标题】:Automatically run specific tests on file change?对文件更改自动运行特定测试?
【发布时间】:2017-05-07 13:14:03
【问题描述】:

我正在寻找一种在更改特定文件时自动运行特定测试的方法,类似于您可以在 Ruby on Rails 中使用 Guardfile 执行的操作。我想知道是否有办法使用 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',
    ];
});

【问题讨论】:

  • 可以使用 cron 作业,但最低频率为每分钟
  • 如何在使用宅基地的开发环境中做到这一点?任何指向示例的链接将不胜感激
  • 据我所知,与您在其他任何地方设置 cron 作业的方式相同

标签: php laravel phpunit tdd laravel-5.3


【解决方案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

现在,只要src/ 中的 php 文件发生更改,phpunit 就会运行,假设您在后台运行了 grunt watch 任务。您当然可以在 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

如果您的用户测试跨越多个目录,则使用测试套件是更好的选择。因此,如果您想在测试/用户和测试/验证等中运行所有测试文件,您的 phpunit.xml 文件中有一个测试套件可以运行这些文件。比如:

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

【讨论】:

  • 我正在查看 2 个链接,但我只是不确定如何应用我的问题中的示例。如果您可以使用我的示例更新您的答案,我会接受这个答案。
  • 没问题。你能澄清一下你需要什么,它是为了匹配某些文件吗?因此,如果一个文件发生更改,会导致针对其他相关/特定文件运行测试?
  • 是的,匹配某些文件并导致某些测试运行。请看一下我的问题中的示例。例如,当UsersController 更新时,运行tests/Users 目录中的所有测试。如果您能匹配我在问题上发布的相同正则表达式,我将不胜感激。感谢您的帮助
  • 我问了另一个相关问题here
猜你喜欢
  • 1970-01-01
  • 2019-04-27
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
相关资源
最近更新 更多