本文翻译自:How to run single test method with phpunit?
I am struggling to run a single test method named testSaveAndDrop
in the file escalation/EscalationGroupTest.php
with phpunit
. 我奋力奔跑名为单个测试方法testSaveAndDrop
在文件escalation/EscalationGroupTest.php
与phpunit
。 I tried the following combinations: 我尝试了以下组合:
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 all test methode in the file escalation/EscalationGroupTest.php
are executed. 在每种情况下,都执行文件escalation/EscalationGroupTest.php
中的所有测试方法。 How to select just ONE method instead? 如何只选择一种方法呢?
The name of the class is EscalationGroupTest
and the version of phpunit
is 3.2.8. 该类的名称是EscalationGroupTest
,而phpunit
的版本是3.2.8。
#1楼
参考:https://stackoom.com/question/1LuvV/如何使用phpunit运行单一测试方法
#2楼
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.html
#3楼
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
#4楼
以下命令将完全执行testSaveAndDrop
测试。
phpunit --filter '/::testSaveAndDrop$/' escalation/EscalationGroupTest.php
#5楼
The reason your tests are all being run is that you have the --filter
flag after the file name. 测试全部运行的原因是文件名后面有--filter
标志。 PHPUnit is not reading the options at all and so is running all the test cases. PHPUnit根本不读取选项,因此正在运行所有测试用例。
From the help screen: 在帮助屏幕上:
Usage: phpunit [options] UnitTest [UnitTest.php]
phpunit [options] <directory>
So move the --filter
argument before the test file that you want as mentioned in @Alex and @Ferid Mövsümov answers. 因此,如--filter
和@FeridMövsümov答案中所述,将--filter
参数移至所需的测试文件之前。 And you should only have the test that you want run. 而且,您应该只具有要运行的测试。
#6楼
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()
{
}