1
votes

I'm using php artisan test to execute my tests, but now I'm having too many of them and I'd like to be able to choose which one to run. I am familiar with test groups in PHPUnit, I just don't know how to apply this in the case of Laravel, since phpunit.xml is dynamically generated here.

Thanks

2
Did you find a solution for this. I am also looking at something simiar?Naveen

2 Answers

0
votes

You can group PHPUnit tests with the @group annotation. I suspect you can refer to this group from artisan as well: http://laravel.com/docs/artisan/commands#unit-tests

You can put the @group on the test-class, or just a test-method. You can put multiple groups on a class/method. This way you can organize them.

0
votes

It isn't possible to do this without modifying several of Laravel's core files. I was in need of this functionality desperately, and so went ahead and added the functionality to Laravel.

The following is for Laravel 3: Open up Laravel/cli/tasks/tests/runner.php, and replace the bundle function with the following:

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

Then, replace the test function with the following:

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);
}

The solution is a little hacky, but it gets the job done.

In short, to run a specific test group for PHPUnit, run the following from the command line:

php artisan test group_name_here

This will run the group from a file with the same name as the group (groupname.test.php). To run a specific group within a specific file, specify the group name and then the first part of the file name:

php artisan test mygroupname myfilename

You could always add functionality to allow it to run the group name from all files in the directory too, I guess.

I hope this helps anyone else out there that need the functionality :)