【问题标题】:Run only one unit test from a test suite in laravel在 laravel 的测试套件中只运行一个单元测试
【发布时间】:2016-12-13 18:19:30
【问题描述】:

使用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);
    }    
}

【问题讨论】:

    标签: php laravel testing phpunit


    【解决方案1】:

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

    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$/'
    

    【讨论】:

    • 太棒了!谢谢扎恩 :)
    • 很好解释。但是对于 laravel,如果您的项目没有配置 phpunit 命令,您将不得不使用 ./vendor/bin/phpunit
    【解决方案2】:

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

    --filter <pattern>          Filter which tests to run.
    
    --testsuite <name,...>      Filter which testsuite to run
    

    【讨论】:

    • 谢谢@Ken Pi,正是我要找的东西
    【解决方案3】:

    对于 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 - 定义测试方法的路径

    【讨论】:

      【解决方案4】:
      php artisan test --filter UserTest
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-25
        • 2014-10-13
        • 2016-11-07
        • 2017-10-19
        • 1970-01-01
        • 2013-07-29
        相关资源
        最近更新 更多