Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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);
    }    
}
like image 825
Imanuel Pardosi Avatar asked Aug 08 '16 04:08

Imanuel Pardosi


People also ask

How do I run a single PHPUnit test?

Running a Single PHPUnit Test Case. To Run a single PHPUnit Test Case on a method within the file: Open your PHPUnit Test Case file in the editor. Place your cursor on the method you wish to test, right-click and select Run As | PHP Unit Test.


1 Answers

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$/'
like image 189
Zayn Ali Avatar answered Oct 20 '22 01:10

Zayn Ali