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

标签 php laravel testing phpunit

使用 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* 开头的函数

它基本上是一个子字符串匹配。如果您想排除所有其他方法,请像这样使用 $ end of string token

phpunit --filter '/testSave$/'

关于php - 在 laravel 的测试套件中只运行一个单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38821326/

相关文章:

spring-mvc - Spring mockMvc 在使用 ExceptionHandler 时抛出错误

php - 正则表达式 : same character 3 times

php - jp图 : how set label for group bar column chart in php

php - $_SERVER ['HTTP_HOST' ] 和 $_SERVER ['REQUEST_URI' ] 对于 WordPress 的替代方案是什么

php - 如何删除 Laravel 中的重复行

php - Laravel 克隆查询字符串

PHP 执行外部命令 - 当需要用户名和密码时如何执行

php - Laravel - 如何从 auth 用户名匹配的另一个表中获取 profile_picture?

eclipse - 非终止 java ui 测试?

java - Apache poi - 在现有 .xlsx 文件中添加新页面