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

在尝试运行PHP的PHPUnit测试时,作者发现指定单一测试方法时,所有测试方法都会被执行。文章讨论了如何正确地通过命令行选择特定的测试方法,包括在命令中放置`-t`或`--test`参数的位置,以及使用注解来标记测试。文中给出了不同版本phpunit的命令示例,并强调了参数放置顺序的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自: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.phpphpunit 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.h​​tml


#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()
{
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值