在 laravel 的测试套件中只运行一个单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38821326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run only one unit test from a test suite in laravel
提问by Imanuel Pardosi
Using phpunitcommand Laravel will run all unit tests in our project.
How to run one or specific Unit Tests in Laravel 5.1?
使用phpunit命令 Laravel 将运行我们项目中的所有单元测试。如何在 中运行一个或特定的单元测试Laravel 5.1?
I just want to run testFindTokenfrom my test suite.
我只想testFindToken从我的测试套件中运行。
<?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);
}
}
回答by Zayn Ali
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 testSaveand another function named testSaveAndDropand you pass testSaveto the --filterlike so
如果你有一个命名的函数testSave和另一个命名的函数,testSaveAndDrop并且你传递testSave给--filter这样的
phpunit --filter testSave
it will also run testSaveAndDropand any other function that starts with testSave*
它也将运行testSaveAndDrop以及任何其他以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$/'
回答by Doan Thai
You should run ./vendor/bin/phpunit --helpto get all options with phpunit. And you can run phpunit with some option below to run specific method or class.
您应该运行./vendor/bin/phpunit --help以获取 phpunit 的所有选项。您可以使用下面的一些选项运行 phpunit 来运行特定的方法或类。
--filter <pattern> Filter which tests to run.
--testsuite <name,...> Filter which testsuite to run

