如何在 laravel 中使用 phpunit 测试特定测试 class

how to test specific test class using phpunit in laravel

我想在我的项目中测试特定测试Class,因为有很多测试class失败了,我只想一次测试一个Class。

我在以下文件夹 \test\repositories\ApplicationVersionFormat.php 中创建了测试 Class:

<?php
use App\Repositories\ApplicationVersionFormat;

class ApplicationVersionFormatTest extends \TestCase
{
  public function testValidFormat()
  {
    $version = '1.2.3';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(true,$appVersion->isValidVersion($version));
  }

  public function testInvalidFormat()
  {
    $version = '11.2.3';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(false,$appVersion->isValidVersion($version));
  }

  public function testInvalidFormat2()
  {
    $version = '1.22.3';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(false,$appVersion->isValidVersion($version));
  }

  public function testInvalidFormat3()
  {
    $version = '1.2.33';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(false,$appVersion->isValidVersion($version));
  }

  public function testInvalidFormat4()
  {
    $version = '11.22.33';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(false,$appVersion->isValidVersion($version));
  }
}

所以我尝试了以下命令,但是 none 有效:

有什么帮助吗?谢谢

您可以通过 this answer 解决相关问题。

只需用 @group 注释标记 class 并用 --group <group_name>

标记 运行 PHPUnit

更新

您使用 --filter 的命令不完整。试试这个:phpunit --filter AppVersionTest "repositories\ApplicationVersionFormat.php"

在尝试了几种方法后,我发现我不需要包含文件夹来测试特定的测试class。这对我有用,它在 class 上运行所有测试:

phpunit --filter ApplicationVersionFormatTest

我认为这是因为我的 ApplicationVersionFormatTest 扩展了 TestCase 和 return 应用程序实例,它作为 Laravel 的所有组件的 "glue"。

phpunit --filter <relative_path_to_file> 例如,要测试的文件是,test/repos/EloquentPushTest.phptest/repos/EloquentWebTest.php 用于测试 EloquentWebTest.php

使用phpunit --filter ./repos/ElqouentWebpush

运行 在 laravel 中通过多种方式进行 phpunit 测试..

vendor/bin/phpunit --filter methodName className pathTofile.php

vendor/bin/phpunit --filter 'namespace\directoryName\className::methodName'

测试单曲class:

vendor/bin/phpunit tests/Feature/UserTest.php
vendor/bin/phpunit --filter  tests/Feature/UserTest.php
vendor/bin/phpunit --filter 'Tests\Feature\UserTest'
vendor/bin/phpunit --filter 'UserTest' 

测试单一方法:

 vendor/bin/phpunit --filter testExample 
 vendor/bin/phpunit --filter 'Tests\Feature\UserTest::testExample'
 vendor/bin/phpunit --filter testExample UserTest tests/Feature/UserTest.php

对于来自命名空间内所有 class 的 运行 测试:

vendor/bin/phpunit --filter 'Tests\Feature'

更多方式运行测试see more

使用示例:

php phpunit-5.7.27.phar -c app/ "src/package/TestBundle/Tests/Controller/ExampleControllerTest.php"

使用phpunit.xml

@group 选项可用于 class 实体和方法。

class一个

/**
 * @group active
 * */
class ArrayShiftRightTest extends TestCase
{
}

class B

/**
 * @group inactive
 * */
class BinaryGapTest extends TestCase
{    
}

phpunit.xml

<groups>
    <include>
        <group>active</group>
    </include>
    <exclude>
        <group>inactive</group>
    </exclude>
</groups>

属于active组的classes将被执行。

使用artisan做同样的事情 运行:

php artisan test --filter ApplicationVersionFormatTest

对于较新的版本,这可能有效

php artisan test --filter {MethodName}

// for instance
php artisan test --filter test_example

php artisan test --filter {ClassName}::{MethodName}

//for instance
php artisan test --filter ExampleTest::test_example

对于laravel 8

php artisan test --filter ExampleTest