如何在更新时自动运行单个phpunit测试? [英] How to automatically run a single phpunit test when it is updated?

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

问题描述

我正在使用Laravel 5.3,不幸的是,当您运行 gulp tdd 时,对1个文件的更改运行整个测试套件,现在需要将近2分钟。参考这篇文章,我开始使用 Grunt 在特定文件更改时运行特定测试。下面的示例Gruntfile:

Gruntfile.js:

  var phpunit ='vendor / bin / phpunit'; 
grunt.initConfig({
pkg:grunt.file.readJSON('package.json'),
watch:{
HomeSrc:{
files:[
'app / Http / ** / HomeController.php',
'resources / views / home / ** / *。php'
],
tasks:['HomeTests' ]
},
shell:{
HomeTests:{command:phpunit +'tests / Home'},
}
});

然而,现在我的Gruntfile变得很长,我想在它们运行时运行特定的测试文件

问题


  1. 更有效的方法来做到这一点? (更好地组织Grunfile或使用Gulp代替)
  2. 如何在文件更改时运行特定测试?



示例:
tests / Home / IndexTest.php 被更改时,自动运行 vendor / bin / phpunit tests / Home / IndexTest.php

解决方案

匹配的文件名并动态设置一个变量以用作单元测试文件。这应该涵盖所有基本映射,其中测试类名称与它所在的文件名称完全相同,并且支持命名空间,因此不是所有具有相同类名称的测试文件都将被过滤器拾取。



示例:

  grunt.initConfig({
// ..被剪切..
unitTestFile:'to_be_replaced',
watch:{
php:{b $ b files:[tests / ** / *。php],
tasks :[shell:unitTest],
options:{
spawn:false
}
}
},
shell:{
unitTest:{
command:phpunit --filter<%= unitTestFile%>
}
}

grunt.loadNpmTasks('grunt-shell' );

grunt.event.on('watch',function(action,filepath){
if(grunt.file.isMatch(grunt.config('watch.php.files' ),filepath)){
var testFile = filepath.repla ce(/ \\ / g,'\\\\\');
grunt.config('unitTestFile',testFile.replace(/。php /,''));
}
});
};

因此,名为的文件tests\unit\ApplicationTest.php ,并且在 tests \ unit 的命名空间中,如果已更改,现在将其作为测试运行。结果命令为:

  phpunit --filter tests\\unit\\ApplicationTest //仅在此运行命名空间


I am using Laravel 5.3 and unfortunately when you run gulp tdd, a change to 1 file runs the entire test suite which now takes nearly 2 minutes. With reference to this post, I started using Grunt to run specific tests when specific files are changed. Sample Gruntfile below:

Gruntfile.js:

var phpunit = 'vendor/bin/phpunit ';
grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    watch: {
        HomeSrc: {
            files: [
                'app/Http/**/HomeController.php',
                'resources/views/home/**/*.php'
            ],
            tasks: ['HomeTests']
        },
    shell: {
        HomeTests: { command: phpunit + 'tests/Home' },
    }
});

However, now my Gruntfile is getting pretty long and I would like to run specific test files when they are changed.

Questions

  1. Is there a more efficient way to do this? (better organization of the Grunfile or using Gulp instead)
  2. How can I run a specific test when its file is changed?

Example: When tests/Home/IndexTest.php is changed, automatically run vendor/bin/phpunit tests/Home/IndexTest.php

解决方案

OK, to handle this you will need to catch the matched file name and dynamically set a variable to use as the unit test file. This should cover all basic mappings where the test class name is exactly the same name as the file name it is within, and supports namespaces so not all test files with the same class name will be picked up by the filter.

Example:

grunt.initConfig({
    // .. snipped ..
    unitTestFile: 'to_be_replaced',
    watch: {
        php: {
            files: ["tests/**/*.php"],
            tasks: ["shell:unitTest"],
            options: {
                spawn: false
            }
        }
    },
    shell: {
        unitTest: {
            command: "phpunit --filter <%= unitTestFile %>"
        }
    }

    grunt.loadNpmTasks('grunt-shell');

    grunt.event.on('watch', function (action, filepath) {
        if (grunt.file.isMatch(grunt.config('watch.php.files'), filepath)) {
            var testFile = filepath.replace(/\\/g, '\\\\');
            grunt.config('unitTestFile', testFile.replace(/.php/, ''));
        }
    });
};

So, a file named tests\unit\ApplicationTest.php and within a namespace of tests\unit if changed will now run that as a test. The resulting command being:

phpunit --filter tests\\unit\\ApplicationTest // only runs in this namespace

这篇关于如何在更新时自动运行单个phpunit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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