php7 phpunittest,php - 如何用phpunit运行单一测试方法?

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

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

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

在每种情况下,执行文件EscalationGroupTest中的所有测试方法。 如何选择一种方法呢?

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

Alex asked 2019-03-09T03:30:32Z

11个解决方案

304 votes

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

phpunit --filter testSaveAndDrop EscalationGroupTest escalation/EscalationGroupTest.php

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

Alex answered 2019-03-09T03:30:45Z

175 votes

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

/**

* @group failing

* Tests the api edit form

*/

public function testEditAction()

然后运行它

phpunit --group failing

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

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

/**

* @group failing

* @group bug2204

*/

public function testSomethingElse()

{

}

iamtankist answered 2019-03-09T03:31:32Z

97 votes

这是更通用的答案:

如果您确定方法名称是唯一的,则只能按方法名称进行筛选(这对我有用)

phpunit --filter {TestMethodName}

但是,指定文件路径/引用也更安全

phpunit --filter {TestMethodName} {FilePath}

例:

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

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

Mahmoud Zalt answered 2019-03-09T03:32:23Z

27 votes

以下命令将完全执行testSaveAndDrop测试。

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

Farid Movsumov answered 2019-03-09T03:32:51Z

15 votes

所以,这样的事情

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

没有=和'

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

sectus answered 2019-03-09T03:33:39Z

7 votes

如果您在netbeans中,可以右键单击测试方法并单击“运行聚焦测试方法”。

ea880c843aa018b932ec68d9b0ff3ea6.png

Tony answered 2019-03-09T03:34:10Z

6 votes

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

phpunit tests/{testfilename}

例如:

phpunit tests/StackoverflowTest.php

如果您想在Laravel 5.5中运行单个测试用例,请尝试

vendor/bin/phpunit tests/Feature/{testfilename}

vendor/bin/phpunit tests/Unit/{testfilename}

例如:

vendor/bin/phpunit tests/Feature/ContactpageTest.php

vendor/bin/phpunit tests/Unit/ContactpageTest.php

Amit Yadav answered 2019-03-09T03:34:51Z

4 votes

您的测试全部运行的原因是您在文件名后面有--filter标志。 PHPUnit根本没有读取选项,因此运行所有测试用例。

从帮助屏幕:

Usage: phpunit [options] UnitTest [UnitTest.php]

phpunit [options]

因此,请在@Alex和@中提到的测试文件之前移动--filter参数@FeridMövsümov回答。 而且你应该只进行你想要运行的测试。

Schleis answered 2019-03-09T03:35:38Z

3 votes

通过很多方式在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'

更多方式运行测试看到更多

Jignesh Joisar answered 2019-03-09T03:36:44Z

1 votes

如果您使用的是XML配置文件,则可以在phpunit标记内添加以下内容:

nameToInclude

nameToExclude

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

user276648 answered 2019-03-09T03:37:19Z

0 votes

您必须使用--filter来运行单个测试方法php phpunit --filter "/::testMethod( .*)?$/" ClassTest ClassTest.php

上面的过滤器将单独运行testMethod。

Umair Anwar answered 2019-03-09T03:37:56Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值