如何在Laravel中使用PHPUnit测试特定测试类

150

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

我已经在以下文件夹中创建了测试类\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"。=> 未执行任何测试!

有人可以帮忙吗?谢谢。


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

165

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

phpunit --filter ApplicationVersionFormatTest

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


1
除了使用phpunit命令,您还可以使用test Artisan命令来运行测试。Artisan测试运行器提供详细的测试报告,以便于开发和调试: - Yohanim
我尝试过放整个测试命名空间,但它没有起作用;你应该只传递类名才能使其工作。 - shamaseen
您上次对此答案进行投票是在2021年5月26日13:51。除非此答案被编辑,否则您的投票现已锁定。我们又来了 :) 两年后再次回归。 - Moode Osman

119

有多种方式在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'

查看更多方法运行测试,请点击此处


vendor/bin/phpunit --filter tests/Feature/UserTest.php 返回测试已在我的情况下执行。 - shamaseen
1
非常感谢你提供的这个 vendor/bin/phpunit tests/Feature/UserTest.php,最终在众多其他选项中,这个对我起了作用。因为我严格地想要测试单个文件,使用过滤器会进入模糊搜索,然后还会测试包含上述情况中的交易名称的类似测试,但你的解决方案是万无一失的。谢谢伙计。 - undefined

91

使用artisan运行相同的操作:

php artisan test --filter ApplicationVersionFormatTest

1
在我的情况下,它可以使用默认的Feature和Unit文件夹,但如果我创建一个新的测试套件,它就无法工作。 - Berni
@Berni请确保你新添加的测试类以Test结尾,否则PHPUnit将无法识别它。 - osemec
@Berni请确保将新的测试套件添加到您的phpunit.xml文件中。 - tbone1000

30

对于更新版本,这可能会起作用

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

1
过滤目录怎么样? - Berni

10

你可以通过查看这个问题的答案来解决这个问题。

只需在类上标记@group注释,并使用--group <group_name>运行PHPUnit即可。

更新

你的带有--filter的命令不完整。尝试这样做:phpunit --filter AppVersionTest "repositories\ApplicationVersionFormat.php"


5

针对 Laravel 8

php artisan test --filter ExampleTest

1

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


1

使用示例:

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

0

使用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>

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


0
在Laravel 10中,
对于特定的测试类:
php artisan test --filter UserTest

对于UserTest的特定方法测试:
php artisan test --filter UserTest::test_create_user

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接