1

我正在使用php artisan test来执行我的测试,但现在我的测试太多了,我希望能够选择运行哪一个。我熟悉 PHPUnit 中的测试组,我只是不知道如何在 Laravel 的情况下应用它,因为 phpunit.xml 是在这里动态生成的。

谢谢

4

2 回答 2

0

如果不修改 Laravel 的几个核心文件,就不可能做到这一点。我非常需要这个功能,所以继续将这个功能添加到 Laravel。

以下是针对 Laravel 3 的:打开 Laravel/cli/tasks/tests/runner.php,并将 bundle 函数替换为以下内容:

public function bundle($bundles = array())
{
    if (count($bundles) == 0)
    {
        $bundles = Bundle::names();
    }

    $is_bundle = false;
    $this->base_path = path('sys').'cli'.DS.'tasks'.DS.'test'.DS;

    foreach ($bundles as $bundle)
    {
        // To run PHPUnit for the application, bundles, and the framework
        // from one task, we'll dynamically stub PHPUnit.xml files via
        // the task and point the test suite to the correct directory
        // based on what was requested.
        if (is_dir($path = Bundle::path($bundle).'tests'))
        {
            $this->stub($path);

            $this->test();
            $is_bundle = true;
        }
    }

    if (!$is_bundle)
    {
        $this->stub($path);

        // Run a specific test group
        $this->test($bundles[0], $bundles[1]);
    }
}

然后,将测试功能替换为以下内容:

protected function test($group = null, $file = null)
{
    // We'll simply fire off PHPUnit with the configuration switch
    // pointing to our requested configuration file. This allows
    // us to flexibly run tests for any setup.
    $path = 'phpunit.xml';

    // fix the spaced directories problem when using the command line
    // strings with spaces inside should be wrapped in quotes.
    $esc_path = escapeshellarg($path);

    $group_string = '';

    if ($group)
    {
        $group_string = '--group ' . $group . ' ';

        if ($file)
        {
            $group_string .= path('app') . 'tests/' . $file . '.test.php';
        }
        else
        {
            $group_string .= path('app') . 'tests/' . $group . '.test.php';
        }
    }

    passthru('phpunit --configuration '.$esc_path.' '.$group_string, $status);

    @unlink($path);

    // Pass through the exit status
    exit($status);
}

解决方案有点老套,但它完成了工作。

简而言之,要为 PHPUnit 运行特定的测试组,请从命令行运行以下命令:

php artisan test group_name_here

这将从与组 (groupname.test.php) 同名的文件中运行组。要在特定文件中运行特定组,请指定组名,然后指定文件名的第一部分:

php artisan test mygroupname myfilename

我猜你总是可以添加功能以允许它从目录中的所有文件中运行组名。

我希望这可以帮助其他需要该功能的人:)

于 2013-05-16T11:09:04.920 回答
0

您可以使用@group 注释对 PHPUnit 测试进行分组。我怀疑你也可以从工匠那里参考这个小组:http: //laravel.com/docs/artisan/commands#unit-tests

您可以将@group 放在测试类上,或者只是一个测试方法。您可以将多个组放在一个类/方法上。这样您就可以组织它们。

于 2013-01-24T13:18:18.477 回答