【问题标题】:How to choose which PHPUnit tests to run in Laravel 3.x?如何选择在 Laravel 3.x 中运行哪些 PHPUnit 测试?
【发布时间】:2013-01-08 05:43:32
【问题描述】:

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

谢谢

【问题讨论】:

  • 您找到解决方案了吗?我也在看类似的东西?

标签: phpunit laravel laravel-3


【解决方案1】:

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

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

【讨论】:

  • 我有点假设 bundle-name 可以解决问题......现在我快速说了一下,我读了一点 laravel。然而这应该是可能的,因为这个测试包 (github.com/laravel/tests/blob/master/cases/database.test.php) 使用这个 @group 注释。也许这可以解决问题? php 工匠测试: (codehappy.daylerees.com/unit-testing).
  • 我刚刚尝试了 php artisan test:my_group_name 并且我只是得到“对不起,我找不到那个方法!”
  • 对不起,我浏览了 laravel 的源代码,但没有看到传递额外参数的选项。配置文件 phpunit.xml 是动态生成的,因此也没有选项。也许更多知识渊博的 laravel 用户可以帮助你
【解决方案2】:

不修改 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]);
    }
}

然后,将 test 函数替换为以下内容:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2017-10-19
    • 2015-10-14
    • 1970-01-01
    • 2011-09-21
    • 2012-01-08
    • 2016-08-18
    相关资源
    最近更新 更多