30

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 App\Models\AccessToken;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class AccessTokenRepositoryTest extends TestCase
{
    use WithoutMiddleware;

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

        $this->accessToken = factory(AccessToken::class);
        $this->repo_AccessToken = app()->make('App\Repositories\AccessTokenRepository');
    }

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

5 Answers 5

62

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$/'
Sign up to request clarification or add additional context in comments.

1 Comment

Well explained. But for laravel if phpunit command is not configured with your project you will have to use ./vendor/bin/phpunit instead.
21
php artisan test --filter UserTest

Comments

16

You should run ./vendor/bin/phpunit --help to get all options with phpunit. And you can run phpunit with some option below to run specific method or class.

--filter <pattern>          Filter which tests to run.

--testsuite <name,...>      Filter which testsuite to run

Comments

3

For windows environment this works for me:

in console:

vendor\bin\phpunit --filter login_return_200_if_credentials_are_fine tests/Unit/Http/Controllers/AuthControllerTest.php

WHERE

login_return_200_if_credentials_are_fine - your test method tests/Unit/Http/Controllers/AuthControllerTest.php - path where test method defined

Comments

3

Late to the party but it might help someone.

To test specific test file

php artisan test --filter AccessTokenRepositoryTest

To test specific case of that file

php artisan test --filter AccessTokenRepositoryTest::testFindToken

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.