Skip to content
Advertisement

Run only one unit test from a test suite in laravel

Using phpunit command Laravel will run all unit tests in our project. How to run one or specific Unit Tests in Laravel 5.1?

I just want to run testFindToken from my test suite.

<?php

use Mockery as m;
use AppModelsAccessToken;
use IlluminateFoundationTestingWithoutMiddleware;

class AccessTokenRepositoryTest extends TestCase
{
    use WithoutMiddleware;

    public function setUp()
    {
        parent::setUp();

        $this->accessToken = factory(AccessToken::class);
        $this->repo_AccessToken = app()->make('AppRepositoriesAccessTokenRepository');
    }

    public function testFindToken()
    {
        $model = $this->accessToken->make();
        $model->save();
        $model_accessToken = $this->repo_AccessToken->findToken($model->id);
        $this->assertInstanceOf(IlluminateDatabaseEloquentModel::class, $model);
        $this->assertNotNull(true, $model_accessToken);
    }    
}

Advertisement

Answer

Use this command to run a specific test from your test suite.

phpunit --filter {TestMethodName}

If you want to be more specific about your file then pass the file path as a second argument

phpunit --filter {TestMethodName} {FilePath}

Example:

phpunit --filter testExample path/to/filename.php

Note:

If you have a function named testSave and another function named testSaveAndDrop and you pass testSave to the --filter like so

phpunit --filter testSave

it will also run testSaveAndDrop and any other function that starts with testSave*

it is basically a sub-string match. If you want to exclude all other methods then use $ end of string token like so

phpunit --filter '/testSave$/'
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement