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条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集