Run only one unit test from a test suite in laravel
Solution 1
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$/'
Solution 2
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
Solution 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
Solution 4
php artisan test --filter UserTest
Imanuel Pardosi
Updated on July 11, 2022Comments
-
Imanuel Pardosi almost 2 years
Using
phpunit
command Laravel will run all unit tests in our project. How to run one or specific Unit Tests inLaravel 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); } }