ChatGPT解决这个技术问题 Extra ChatGPT

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

我正在努力使用 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 中的 all 测试方法。如何只选择一种方法?

类的名称是 EscalationGroupTestphpunit 的版本是 3.2.8。

你的测试类的类名是什么?

r
rlorenzo

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

phpunit --filter testSaveAndDrop EscalationGroupTest escalation/EscalationGroupTest.php

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

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

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

是否需要编写 EscalationGroupTest?它有什么用?
好的,我知道了,这是班级名称
它也会运行名为 testSaveAndDrop* 的测试方法(例如:testSaveAndDropSomething)。要准确运行 testSaveAndDrop,请使用 --filter '/::testSaveAndDrop$/'
@mujaffars 我对此表示怀疑,由于空格,“我知道了”不是 php 中的有效标识符。
现在这行不通。只是 phpunit --filter methodName path/to/testing/file,没有 ClassName
i
iamtankist

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

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

然后运行它

phpunit --group failing

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

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

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

我喜欢这种方法,是否可以通过注释分配多个组? @group failing, Integration
是的当然。但不是逗号分隔。编辑答案以说明这一点。
我更喜欢这种方法...供参考<groups> phpunit.xml 中的元素也可用于过滤测试...在 <phpunit> 下使用类似的内容:<groups><include><group>failing</group></include>< /组>
但问题是 - 运行 SINGLE 测试。此外 - 名为失败的组是您可以使用的最糟糕的。因为在某些测试失败后,您可以运行 phpunit --group failed 运行失败的测试而不给他们组。这很令人困惑。
最佳答案!如果它来自另一个 @depends,则没有 SINGLE 测试,因此您必须查明所有互连的测试并因此使用 @group 注释。
b
boroboris

这是更通用的答案:

如果您确定方法名称是唯一的,您只能按方法名称过滤(这对我有用)

phpunit --filter {TestMethodName}

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

phpunit --filter {TestMethodName} {FilePath}

例子:

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

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


这一点也不奇怪,它基本上是一个子字符串匹配。如果您想要这种行为,请使用字符串结束标记:/testSave$/
如果您只想指定文件路径以便运行该文件中的所有测试怎么办?
@still_dreaming_1 只是不要添加 --filter {TestMethodName} 并且只提供路径。
F
Farid Movsumov

以下命令将执行 exactly testSaveAndDrop 测试。

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

没有为我工作。使用 \b 而不是 $ 终于成功了:--filter '/::testSaveAndDrop\b/'
方法名称结尾 '/::testSaveAndDrop\b/''/::testSaveAndDrop$/' 都像 phpunit --filter ClassName::methodName$phpunit --filter ClassName::methodName\b 一样为我工作(在 Linux 上)
同样在这里:两者都工作(在 Linux 上),加上 / 字符是可选的(PHPUnit 9.5.13)。
@Joe 只是预感:您在 PhpDoc 注释中的方法名称后面有 () 吗?这可能是 $ 不起作用的原因,以及 \b 对两者都有效的原因。
V
Vinay Kaithwas

在我在 laravel 根目录中使用的项目根目录中运行它。

vendor/bin/phpunit --filter 'Your method name'

具有自定义方法名称的示例。

 /** @test //Initilize this for custom method name, without test keyword
  *  
  * Test case For Dashboard When User Not logged In it will redirect To login page
  */
  public function only_logged_in_user_see_dashboard()
  {
    $response = $this->get('/dashboard')
                   ->assertRedirect('/login');
  }

带有 test 关键字的示例

/**
* A basic test example.
*
* @return void
*/
 public function testBasicTest()
 {
  $this->assertTrue(true);
 }

J
Jignesh Joisar

以多种方式在 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'

更多方式运行测试see more


a
activatedgeek

所以,像这样

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

没有 = 和有 '

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


不,这不起作用。正在处理类 EscalationGroupTest 中的所有测试。
仍然在运行所有 9 个测试。 phpunit 3.2.8 版
--filter 移到文件名之前,一切正常。
在 PHPUnit 4.8.10 上为我工作
这给了我在 PHPUnit 上的 No tests executed! 4.8.35 @Schleis 您到底是如何更改命令的?
T
Tony

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

https://i.stack.imgur.com/vLzLH.png


R
Robert

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

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

问题是如何运行单一测试方法(不是类)
S
Schleis

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

从帮助屏幕:

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

因此,将 --filter 参数移到您想要的测试文件之前,如@Alex 和 @Ferid Mövsümov 答案中所述。你应该只拥有你想要运行的测试。


我不认为这解决了最初的问题......我遇到了同样的问题,需要在包含许多单元测试的文件中只运行一个测试,而使用过滤器的另外两个答案对我有用
@jfoo OP 命令的问题之一是 --filter 选项位于文件名之后。其他两个答案有正确答案,但没有指出为什么要应用过滤器。
u
user276648

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

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

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


o
oligofren

鉴于你

vendor/bin/phpunit --filter=EscalationGroupTest::testSaveAndDrop

C
Channaveer Hakari

不过我迟到了。但作为个人,我讨厌写整行。

相反,我在 .bash_profile 文件中使用以下快捷方式,确保在添加任何新别名后获取 .bash_profile 文件,否则它将不起作用。

alias pa="php artisan"
alias pu="vendor/bin/phpunit" 
alias puf="vendor/bin/phpunit --filter"

用法:

puf function_name

puf filename

如果你使用 Visual Studio Code,你可以使用下面的包让你的测试变得轻而易举。

Package Name: Better PHPUnit
Link: https://marketplace.visualstudio.com/items?itemName=calebporzio.better-phpunit

然后,您可以在设置中设置键绑定。我在我的 MAC 中使用 Command + T 绑定。

现在,一旦您将光标放在任何功能上,然后使用键绑定,它将自动运行该单个测试。

如果您需要运行整个类,请将光标放在类的顶部,然后使用键绑定。

如果您有任何其他事情,请始终与终端机联系

快乐编码!


U
Umair Anwar

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

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