【问题标题】:how to test specific test class using phpunit in laravel如何在 laravel 中使用 phpunit 测试特定的测试类
【发布时间】:2016-12-31 06:46:45
【问题描述】:

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

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

<?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));
  }
}

所以我尝试了以下命令,但这些命令都不起作用:

  • phpunit "repositories\AppVersionTest"。 => 无法打开文件“test/repositories/AppVersionTest.php”
  • phpunit "test\repositories\AppVersionTest"。 => 无法打开文件“test/repositories/AppVersionTest.php”
  • phpunit --filter "repositories\AppVersionTest"。 => 未执行任何测试!
  • phpunit --testsuite "repositories\AppVersionTest"。 => 未执行任何测试!

有什么帮助吗?谢谢

【问题讨论】:

  • 你执行的命令的输出是什么?
  • 尝试检查 phpunit.xml(.dist) 文件中的某些包含/排除配置。你能发布这个文件吗?

标签: php laravel phpunit laravel-5.2


【解决方案1】:

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

phpunit --filter ApplicationVersionFormatTest

我认为这是因为我的 ApplicationVersionFormatTest 扩展了 TestCase 并返回了用作 Laravel 所有组件的“粘合剂”的应用程序实例。

【讨论】:

  • 除了 phpunit 命令,您还可以使用 test Artisan 命令来运行您的测试。 Artisan 测试运行程序提供详细的测试报告,以简化开发和调试:
【解决方案2】:

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

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

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

用于测试单个类:

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

对于命名空间内所有类的运行测试:

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

更多方式运行测试see more

【讨论】:

    【解决方案3】:

    使用artisan 执行相同的操作:

    php artisan test --filter ApplicationVersionFormatTest
    

    【讨论】:

    • 在我的情况下,它适用于默认的 Feature 和 Unit 文件夹,但如果我创建一个新的测试套件,它就不起作用。
    • @Berni 确保您新添加的测试类以 Test 结尾,否则 PHPUnit 将无法识别它。
    【解决方案4】:

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

    只需用@group 注解标记类并用--group &lt;group_name&gt; 运行PHPUnit

    更新

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

    【讨论】:

      【解决方案5】:

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

      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
      

      【讨论】:

      • 按目录过滤呢?
      【解决方案6】:

      phpunit --filter &lt;relative_path_to_file&gt; 例如,要测试的文件是,test/repos/EloquentPushTest.php,test/repos/EloquentWebTest.php 用于测试EloquentWebTest.php

      使用phpunit --filter ./repos/ElqouentWebpush

      【讨论】:

        【解决方案7】:

        使用示例:

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

        【讨论】:

          【解决方案8】:

          使用 phpunit.xml

          @group 选项可用于类和方法。

          A级

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

          B类

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

          phpunit.xml

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

          属于活跃组的类将被执行。

          【讨论】:

            猜你喜欢
            • 2018-10-23
            • 1970-01-01
            • 1970-01-01
            • 2017-10-19
            • 2014-09-12
            • 2014-09-11
            • 2015-01-24
            • 2017-11-29
            • 2014-09-05
            相关资源
            最近更新 更多