php 如何使用phpunit运行单个测试方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26095051/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:09:25  来源:igfitidea点击:

How to run single test method with phpunit?

phpphpunit

提问by Alex

I am struggling to run a single test method named testSaveAndDropin the file escalation/EscalationGroupTest.phpwith phpunit. I tried the following combinations:

我奋力奔跑名为单个测试方法testSaveAndDrop在文件中escalation/EscalationGroupTest.php使用phpunit。我尝试了以下组合:

phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=escalation/EscalationGroupTest.php::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=EscalationGroupTest.php::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=EscalationGroupTest::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=testSaveAndDrop

In each case alltest methode in the file escalation/EscalationGroupTest.phpare executed. How to select just ONE method instead?

在每种情况下,文件中的所有测试方法escalation/EscalationGroupTest.php都被执行。如何只选择一种方法?

The name of the class is EscalationGroupTestand the version of phpunitis 3.2.8.

类的名称是EscalationGroupTest,版本phpunit是 3.2.8。

回答by Alex

The following command runs the test on a single method:

以下命令在单个方法上运行测试:

phpunit --filter testSaveAndDrop EscalationGroupTest escalation/EscalationGroupTest.php

phpunit --filter methodName ClassName path/to/file.php

For newer versions of phpunit, it is just:

对于较新版本的 phpunit,它只是:

phpunit --filter methodName path/to/file.php

回答by iamtankist

I prefer marking the test in annotation as

我更喜欢在注释中将测试标记为

/**
 * @group failing
 * Tests the api edit form
 */
public function testEditAction()

Then running it with

然后运行它

phpunit --group failing

No need to specify the full path in the command line, but you have to remember removing this before commit, not to clutter the code.

无需在命令行中指定完整路径,但您必须记住在提交之前将其删除,以免使代码混乱。

You may also specify several groups for a single test

您还可以为单个测试指定多个组

/**
  * @group failing
  * @group bug2204 
  */
public function testSomethingElse()
{
}

回答by Mahmoud Zalt

Here's the more generic answer:

这是更通用的答案:

If you are sure the method name is unique you can only filter by method name (this works for me)

如果您确定方法名称是唯一的,则只能按方法名称过滤(这对我有用)

phpunit --filter {TestMethodName}

However it is safer to specify the file path/reference as well

但是,指定文件路径/引用也更安全

phpunit --filter {TestMethodName} {FilePath}

Example:

例子:

phpunit --filter testSaveAndDrop reference/to/escalation/EscalationGroupTest.php

Quick note: I've noticed that if I have a function named testSaveand another function named testSaveAndDropusing command phpunit --filter testSavewill also run testSaveAndDropand any other function that starts with testSave*, it's weird!!

快速说明:我注意到,如果我有一个testSave名为testSaveAndDropusing command的函数并且另一个名为using command 的函数phpunit --filter testSave也将运行testSaveAndDrop并且任何其他以 开头的函数testSave*,这很奇怪!!

回答by Farid Movsumov

Following command will execute exactlytestSaveAndDroptest.

以下命令将执行完全testSaveAndDrop测试。

phpunit --filter '/::testSaveAndDrop$/' escalation/EscalationGroupTest.php

回答by Jignesh Joisar

for run phpunit test in laravel by many way ..

通过多种方式在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 --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 sectus

So, something like this

所以,像这样的事情

phpunit --filter 'EscalationGroupTest::testSaveAndDrop' EscalationGroupTest escalation/EscalationGroupTest.php 

Without =and with '

没有=和有'

https://phpunit.de/manual/3.7/en/textui.html

https://phpunit.de/manual/3.7/en/textui.h​​tml

回答by Connectify_user

  • Run this inside your project root directory i am using in laravel root directory.
  • 在我在 laravel 根目录中使用的项目根目录中运行它。


vendor/bin/phpunit --filter 'Your method name'

Example with custom method name.

带有自定义方法名称的示例。

 /** @test //Initilize this for custom method name, without test keyword
  *  
  * Test case For Dashboard When User Not logged In it will redirect To login page
  */
  public function only_logged_in_user_see_dashboard()
  {
    $response = $this->get('/dashboard')
                   ->assertRedirect('/login');
  }

Example with test keyword

带有 test 关键字的示例

/**
* A basic test example.
*
* @return void
*/
 public function testBasicTest()
 {
  $this->assertTrue(true);
 }

回答by Amit Kumar

You Can try this i am able to run single Test cases

你可以试试这个我可以运行单个测试用例

phpunit tests/{testfilename}

Eg:

例如:

phpunit tests/StackoverflowTest.php

If you want to run single Test cases in Laravel 5.5 Try

如果你想在 Laravel 5.5 中运行单个测试用例试试

vendor/bin/phpunit tests/Feature/{testfilename} 

vendor/bin/phpunit tests/Unit/{testfilename} 

Eg:

例如:

vendor/bin/phpunit tests/Feature/ContactpageTest.php 

vendor/bin/phpunit tests/Unit/ContactpageTest.php

回答by Tony

If you're in netbeans you can right click in the test method and click "Run Focused Test Method".

如果您在 netbeans 中,您可以右键单击测试方法,然后单击“运行重点测试方法”。

Run Focused Test Method menu

运行重点测试方法菜单

回答by Schleis

The reason your tests are all being run is that you have the --filterflag after the file name. PHPUnit is not reading the options at all and so is running all the test cases.

您的测试都在运行的原因是您--filter在文件名后有标志。PHPUnit 根本不读取选项,因此正在运行所有测试用例。

From the help screen:

从帮助屏幕:

 Usage: phpunit [options] UnitTest [UnitTest.php]
        phpunit [options] <directory>

So move the --filterargument before the test file that you want as mentioned in @Alex and @Ferid M?vsümov answers. And you should only have the test that you want run.

因此--filter,如@Alex 和@Ferid M?vsümov 答案中所述,将参数移到您想要的测试文件之前。并且您应该只运行您想要运行的测试。