douxin1956 2017-01-18 21:52
浏览 79
已采纳

如何在更新时自动运行单个phpunit测试?

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

  • 写回答

3条回答 默认 最新

  • dongyi5425 2017-01-19 17:01
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题