我正在努力使用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。


当前回答

下面的命令将精确地执行testSaveAndDrop test。

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

其他回答

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

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

如果你使用的是XML配置文件,你可以在phpunit标签中添加以下内容:

<groups>
  <include>
    <group>nameToInclude</group>
  </include>
  <exclude>
    <group>nameToExclude</group>
  </exclude>
</groups>

参见https://phpunit.de/manual/current/en/appendixes.configuration.html

下面是更一般的答案:

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

phpunit --filter {TestMethodName}

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

phpunit --filter {TestMethodName} {FilePath}

例子:

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

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

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

为在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'

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