我正在努力使用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 --filter 'EscalationGroupTest::testSaveAndDrop' EscalationGroupTest escalation/EscalationGroupTest.php 

没有=和有'

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

其他回答

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

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

然后用

phpunit --group failing

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

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

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

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

下面是更一般的答案:

如果你确定方法名是唯一的,你只能通过方法名进行过滤(这对我来说是有效的)

phpunit --filter {TestMethodName}

不过,指定文件路径/引用也更安全

phpunit --filter {TestMethodName} {FilePath}

例子:

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

快速注意:我已经注意到,如果我有一个名为testSave的函数和另一个名为testSaveAndDrop的函数,使用命令phpunit——filter testSave也将运行testSaveAndDrop和任何其他以testSave*开头的函数,这很奇怪!!

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

phpunit --filter testSaveAndDrop EscalationGroupTest escalation/EscalationGroupTest.php

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

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

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

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

从帮助屏幕:

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

因此,将——filter参数移动到您想要的测试文件之前,如@Alex和 @Ferid Mövsümov回答。您应该只运行您想要运行的测试。