我想在我的项目中测试特定的测试类,因为有很多测试类失败,我只想一次测试一个类。
我在以下文件夹中创建了测试类
\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 --filter ApplicationVersionFormatTest
我认为这是因为我的 ApplicationVersionFormatTest 扩展了 TestCase 并返回应用程序实例,该实例充当 Laravel 所有组件的“粘合剂”。
通过多种方式在 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'
更多方式运行测试查看更多
要使用
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
您可以通过相关问题的这个答案来解决这个问题。
只需使用
@group
注释标记类并使用 --group <group_name>
运行 PHPUnit
更新
您使用
--filter
的命令不完整。试试这个:phpunit --filter AppVersionTest "repositories\ApplicationVersionFormat.php"
对于 Laravel 8
php artisan test --filter ExampleTest
phpunit --filter <relative_path_to_file>
例如,要测试的文件是,test/repos/EloquentPushTest.php
,test/repos/EloquentWebTest.php
用于测试EloquentWebTest.php
使用
phpunit --filter ./repos/ElqouentWebpush
使用示例:
php phpunit-5.7.27.phar -c app/ "src/package/TestBundle/Tests/Controller/ExampleControllerTest.php"
使用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>
属于活动组的类将被执行。
在 Laravel 10 中
对于特定测试类别:
php artisan test --filter UserTest
UserTest的具体方法测试:
php artisan test --filter UserTest::test_create_user