php 如何在laravel中使用phpunit测试特定的测试类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39118117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to test specific test class using phpunit in laravel
提问by Gujarat Santana
I want to test specific testClass in my project since there are a lot of test class that's failure and I just want to test one Class at a time.
我想在我的项目中测试特定的 testClass,因为有很多测试类失败了,我只想一次测试一个类。
I've created the test Class in following folder \test\repositories\ApplicationVersionFormat.php
:
我在以下文件夹中创建了测试类\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));
}
}
so I've tried this follwing command but none of this works :
所以我试过这个以下命令,但这些都不起作用:
phpunit "repositories\AppVersionTest"
. => Cannot open file "test/repositories/AppVersionTest.php"phpunit "test\repositories\AppVersionTest"
. => Cannot open file "test/repositories/AppVersionTest.php"phpunit --filter "repositories\AppVersionTest"
. => No tests executed!phpunit --testsuite "repositories\AppVersionTest"
. => No tests executed!
phpunit "repositories\AppVersionTest"
. =>无法打开文件“test/repositories/AppVersionTest.php”phpunit "test\repositories\AppVersionTest"
. =>无法打开文件“test/repositories/AppVersionTest.php”phpunit --filter "repositories\AppVersionTest"
. =>没有执行测试!phpunit --testsuite "repositories\AppVersionTest"
. =>没有执行测试!
any help? thanks
有什么帮助吗?谢谢
回答by Gujarat Santana
After trying several ways, I found out that I don't need to include the folder to test the specific test class. This works for me it runs all the test on the class:
尝试了几种方法后,我发现我不需要包含文件夹来测试特定的测试类。这对我有用,它在类上运行所有测试:
phpunit --filter ApplicationVersionFormatTest
phpunit --filter ApplicationVersionFormatTest
I think it's because my ApplicationVersionFormatTest extend The TestCase and return application instance which serves as the "glue" for all the components of Laravel.
我认为这是因为我的 ApplicationVersionFormatTest 扩展了 TestCase 并返回了作为 Laravel 所有组件的“胶水”的应用程序实例。
回答by Jignesh Joisar
for run phpunit test in laravel by many ways ..
用于通过多种方式在 Laravel 中运行 phpunit 测试..
vendor/bin/phpunit --filter methodName className pathTofile.php
vendor/bin/phpunit --filter 'namespace\directoryName\className::methodName'
for test single 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'
for test single method:
对于测试单一方法:
vendor/bin/phpunit --filter testExample
vendor/bin/phpunit --filter 'Tests\Feature\UserTest::testExample'
vendor/bin/phpunit --filter testExample UserTest tests/Feature/UserTest.php
for run tests from all class within namespace:
从命名空间内的所有类运行测试:
vendor/bin/phpunit --filter 'Tests\Feature'
for more way run test see more
更多方式运行测试查看更多
回答by BVengerov
You can probably manage that with this answerto a related question.
您可能可以通过对相关问题的回答来解决这个问题。
Just mark the class with @group
annotation and run PHPUnit with --group <group_name>
只需用@group
注释标记类并运行 PHPUnit--group <group_name>
Update
更新
Your command with --filter
is not complete. Try this: phpunit --filter AppVersionTest "repositories\ApplicationVersionFormat.php"
你的命令--filter
不完整。尝试这个:phpunit --filter AppVersionTest "repositories\ApplicationVersionFormat.php"
回答by Bhuvaneshwar Nagaraj
phpunit --filter <relative_path_to_file>
for example , files to test are,test/repos/EloquentPushTest.php
,test/repos/EloquentWebTest.php
for testing EloquentWebTest.php
phpunit --filter <relative_path_to_file>
例如,要测试的文件是test/repos/EloquentPushTest.php
,,test/repos/EloquentWebTest.php
用于测试EloquentWebTest.php
use phpunit --filter ./repos/ElqouentWebpush
用 phpunit --filter ./repos/ElqouentWebpush
回答by joan16v
Example of use:
使用示例:
php phpunit-5.7.27.phar -c app/ "src/package/TestBundle/Tests/Controller/ExampleControllerTest.php"
回答by f_i
Using phpunit.xml
使用phpunit.xml
the @group
option can be used on classes and methods.
该@group
选项可用于类和方法。
class A
A级
/**
* @group active
* */
class ArrayShiftRightTest extends TestCase
{
}
class B
B级
/**
* @group inactive
* */
class BinaryGapTest extends TestCase
{
}
in phpunit.xml
在 phpunit.xml
<groups>
<include>
<group>active</group>
</include>
<exclude>
<group>inactive</group>
</exclude>
</groups>
the classes that belongs to group active will be executed.
将执行属于组 active 的类。