我正在努力使用phpunit在文件escalation/EscalationGroupTest.php中运行一个名为testSaveAndDrop的测试方法。我尝试了以下组合:

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

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

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


当前回答

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

phpunit tests/{testfilename}

Eg:

phpunit tests/StackoverflowTest.php

如果您想在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

其他回答

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

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

然后用

phpunit --group failing

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

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

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

考虑到你

vendor/bin/phpunit --filter=EscalationGroupTest::testSaveAndDrop

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

phpunit tests/{testfilename}

Eg:

phpunit tests/StackoverflowTest.php

如果您想在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

不过我来晚了。但就我个人而言,我讨厌写整句话。

相反,我在.bash_profile文件中使用以下快捷方式,以确保在添加任何新别名之后源文件为.bash_profile,否则它将无法工作。

alias pa="php artisan"
alias pu="vendor/bin/phpunit" 
alias puf="vendor/bin/phpunit --filter"

用法:

puf function_name

puf filename

如果你使用Visual Studio Code,你可以使用下面的包来简化你的测试。

Package Name: Better PHPUnit
Link: https://marketplace.visualstudio.com/items?itemName=calebporzio.better-phpunit

然后,您可以在设置中设置键绑定。我在MAC中使用Command + T绑定。

现在,一旦您将光标放在任何函数上,然后使用键绑定,那么它将自动运行单个测试。

如果需要运行整个类,则将游标放在类的顶部,然后使用键绑定。

如果你有任何其他事情,那么总是与终端

编码快乐!

为在laravel中运行phpunit测试提供了多种方法。

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

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

对于测试单类:

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'

有关更多方式运行测试,请参阅更多