在laravel中的测试套件中只运行一个单元测试


Run only one unit test from a test suite in laravel

使用phpunit命令Laravel将运行我们项目中的所有单元测试。如何在Laravel 5.1中运行一个或特定的单元测试?

我只想从我的测试套件中运行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);
    }    
}

使用此命令从测试套件中运行特定测试。

phpunit --filter {TestMethodName}

如果你想更具体地了解你的文件,那么把文件路径作为第二个参数

phpunit --filter {TestMethodName} {FilePath}

示例:

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

注意:

如果您有一个名为testSave的函数和另一个名称为testSaveAndDrop的函数,并且您将testSave传递给--filter像这样

phpunit --filter testSave

它还将运行testSaveAndDrop和以testSave* 开始的任何其他功能

它基本上是一个子字符串匹配。如果你想排除所有其他方法,那么使用$字符串结束标记,比如

phpunit --filter '/testSave$/'

您应该运行./vendor/bin/phpunit --help来获得phpunit的所有选项。您可以使用下面的一些选项运行phpunit来运行特定的方法或类。

--filter <pattern>          Filter which tests to run.
--testsuite <name,...>      Filter which testsuite to run
php artisan test --filter UserTest

对于windows环境,这对我来说很有效:

控制台中:

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

其中

login_return_200_if_credentials_are_fine-您的测试方法tests/Unit/Http/Controllers/AuthControllerTest.php-测试方法定义的路径

派对迟到了,但这可能会对某人有所帮助。

测试特定测试文件

php手工测试--筛选AccessTokenRepositoryTest

测试该文件的具体情况

php artisan test--filter AccessTokenRepositoryTest::testFindToken